debian: git-build-package config
[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 = NULL;
64         struct in_addr inp;
65         struct stat lease_file_stats;
66         struct macaddr_t *macaddr_p = NULL;
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         fclose(dhcpd_leases);
167         return 0;
168 }
169
170 /* Like strcpy but for field which is separated by white spaces. Number of
171  * first field is 1 and not 0 like C programs should have. Question of
172  * semantics, send mail to author if this annoys. All performance boosts for
173  * this function are well come. */
174 int nth_field(int n, char *dest, const char *src)
175 {
176         int i, j = 0, wordn = 0, len;
177
178         len = strlen(src);
179
180         for (i = 0; i < len; i++) {
181                 if (isspace(src[i])) {
182                         if (!(wordn < n)) {
183                                 dest[j] = '\0';
184                                 break;
185                         }
186                         j = 0;
187                 } else {
188                         if (j == 0) {
189                                 wordn++;
190                         }
191                         if (wordn == n) {
192                                 dest[j] = src[i];
193                         }
194                         j++;
195                 }
196         }
197         return 0;
198 }
199
200 /* dhcpd.conf interesting words */
201 int is_interesting_config_clause(char *s)
202 {
203         if (strstr(s, "range")) {
204                 return 3;
205         } else if (strstr(s, "shared-network")) {
206                 return 1;
207         } else if (strstr(s, "include")) {
208                 return 4;
209         } else {
210                 return 0;
211         }
212 }
213
214 /* FIXME: This spagetti monster function need to be rewrote at least ones. */
215 void parse_config(int is_include, char *config_file,
216                   struct shared_network_t *shared_p)
217 {
218         FILE *dhcpd_config;
219         int i = 0, newclause = true, argument = false, comment =
220             false, braces = 0, quote = false;
221         char *word, c;
222         int braces_shared = 1000;
223         struct in_addr inp;
224         struct range_t *range_p;
225
226         word = safe_malloc(sizeof(char) * MAXLEN);
227
228         if (is_include) {
229                 /* Default place holder for ranges "All networks". */
230                 shared_p->name = shared_networks->name;
231         }
232
233         /* Open configuration file */
234         dhcpd_config = fopen(config_file, "r");
235         if (dhcpd_config == NULL) {
236                 err(EXIT_FAILURE, "parse_config: %s", config_file);
237         }
238 #ifdef POSIX_FADV_WILLNEED
239         posix_fadvise((long) dhcpd_config, 0, 0, POSIX_FADV_WILLNEED);
240         if (errno) {
241                 err(EXIT_FAILURE, "parse_config: fadvise %s", config_file);
242         }
243 #endif                          /* POSIX_FADV_WILLNEED */
244 #ifdef POSIX_FADV_SEQUENTIAL
245         posix_fadvise((long) dhcpd_config, 0, 0, POSIX_FADV_SEQUENTIAL);
246         if (errno) {
247                 err(EXIT_FAILURE, "parse_config: fadvise %s", config_file);
248         }
249 #endif                          /* POSIX_FADV_SEQUENTIAL */
250
251         /* Very hairy stuff begins. */
252         while (!feof(dhcpd_config)) {
253                 c = fgetc(dhcpd_config);
254                 /* Certain characters are magical */
255                 switch (c) {
256                         /* Handle comments if they are not quoted */
257                 case '#':
258                         if (quote == false) {
259                                 comment = true;
260                         }
261                         continue;
262                 case '"':
263                         if (comment == false) {
264                                 quote++;
265                                 /* Either one or zero */
266                                 quote = quote % 2;
267                         }
268                         continue;
269                 case '\n':
270                         /* New line resets comment section, but
271                          * not if quoted */
272                         if (quote == false) {
273                                 comment = false;
274                         }
275                         break;
276                 case ';':
277                         /* Quoted colon does not mean new clause */
278                         if (quote == true) {
279                                 break;
280                         }
281                         if (comment == false && argument != 2
282                             && argument != 4) {
283                                 newclause = true;
284                                 i = 0;
285                         } else if (argument == 2) {
286                                 /* Range ends to ; and this hair in code
287                                  * make two ranges wrote to gether like...
288                                  *
289                                  * range 10.20.30.40 10.20.30.41;range 10.20.30.42 10.20.30.43;
290                                  *
291                                  * ...to be interpreted correctly. */
292                                 c = ' ';
293                         }
294                         continue;
295                 case '{':
296                         if (quote == true) {
297                                 break;
298                         }
299                         if (comment == false) {
300                                 braces++;
301                         }
302                         /* i == 0 detects word that ends to brace like:
303                          *
304                          * shared-network DSL{ ... */
305                         if (i == 0) {
306                                 newclause = true;
307                         }
308                         continue;
309                 case '}':
310                         if (quote == true) {
311                                 break;
312                         }
313                         if (comment == false) {
314                                 braces--;
315                                 /* End of shared-network */
316                                 if (braces_shared == braces) {
317                                         /* FIXME: Using 1000 is lame, but
318                                          * works. */
319                                         braces_shared = 1000;
320                                         shared_p = shared_networks;
321                                 }
322                                 /* Not literally true, but works for this
323                                  * program */
324                                 newclause = true;
325                         }
326                         continue;
327                 default:
328                         break;
329                 }
330
331                 /* Either inside comment or Nth word of clause. */
332                 if (comment == true
333                     || (newclause == false && argument == 0)) {
334                         continue;
335                 }
336                 /* Strip white spaces before new clause word. */
337                 if ((newclause == true || argument != 0) && isspace(c)
338                     && i == 0) {
339                         continue;
340                 }
341                 /* Save to word which clause this is. */
342                 if ((newclause == true || argument != 0)
343                     && (!isspace(c) || quote == true)) {
344                         word[i] = c;
345                         i++;
346                         /* Long word which is almost causing overflow. None
347                          * of words are this long which the program is
348                          * searching. */
349                         if (MAXLEN < i) {
350                                 newclause = false;
351                                 i = 0;
352                                 continue;
353                         }
354                 }
355                 /* See if clause is something that parser is looking for. */
356                 else if (newclause == true) {
357                         /* Insert string end & set state */
358                         word[i] = '\0';
359                         newclause = false;
360                         i = 0;
361
362                         argument = is_interesting_config_clause(word);
363                 }
364                 /* words after range, shared-network or include */
365                 else if (argument != 0) {
366                         word[i] = '\0';
367                         newclause = false;
368                         i = 0;
369
370                         switch (argument) {
371                         case 2:
372                                 /* printf ("range 2nd ip: %s\n", word); */
373                                 range_p = ranges + num_ranges;
374                                 inet_aton(word, &inp);
375                                 argument = 0;
376                                 range_p->last_ip = htonl(inp.s_addr) + 1;
377                                 range_p->count = 0;
378                                 range_p->touched = 0;
379                                 range_p->backups = 0;
380                                 range_p->shared_net = shared_p;
381                                 num_ranges++;
382                                 if (RANGES < num_ranges + 1) {
383                                         RANGES *= 2;
384                                         ranges =
385                                             safe_realloc(ranges,
386                                                          sizeof(struct
387                                                                 range_t) *
388                                                          RANGES);
389                                         range_p = ranges + num_ranges;
390                                 }
391                                 newclause = true;
392                                 break;
393                         case 3:
394                                 /* printf ("range 1nd ip: %s\n", word); */
395                                 range_p = ranges + num_ranges;
396                                 if (!(inet_aton(word, &inp))) {
397                                         /* word was not ip, try
398                                          * again */
399                                         break;
400                                 }
401                                 range_p->first_ip = htonl(inp.s_addr) - 1;
402                                 argument = 2;
403                                 break;
404                         case 1:
405                                 /* printf ("shared-network named: %s\n", word); */
406                                 num_shared_networks++;
407                                 shared_p =
408                                     shared_networks + num_shared_networks;
409                                 shared_p->name = safe_strdup(word);
410                                 shared_p->available = 0;
411                                 shared_p->used = 0;
412                                 shared_p->touched = 0;
413                                 shared_p->backups = 0;
414                                 if (SHARED_NETWORKS <
415                                     num_shared_networks + 2) {
416                                         /* FIXME: make this
417                                          * away by reallocationg
418                                          * more space. */
419                                         errx(EXIT_FAILURE,
420                                              "parse_config: increase default.h SHARED_NETWORKS and recompile");
421                                 }
422                                 argument = 0;
423                                 braces_shared = braces;
424                                 break;
425                         case 4:
426                                 /* printf ("include file: %s\n", word); */
427                                 argument = 0;
428                                 parse_config(false, word, shared_p);
429                                 newclause = true;
430                                 break;
431                         case 0:
432                                 /* printf ("nothing interesting: %s\n", word); */
433                                 argument = 0;
434                                 break;
435                         default:
436                                 warnx("impossible occurred, report a bug");
437                                 assert(0);
438                         }
439                 }
440         }
441         free(word);
442         fclose(dhcpd_config);
443         return;
444 }