1 /* ethers.c
2 *
3 * Copyright (c) 2010 SeaD <sead at deep.perm.ru>
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $Id: ethers.c,v 1.14 2010/07/12 03:46:36 sead Exp $
27 *
28 */
29
30 #include "ipguard.h"
31
32 #define STR_SIZE 128
33 #define ADDR_LEN 64
34
ethers_init(void)35 void ethers_init(void) {
36 FILE *eth_fp;
37 char str[STR_SIZE];
38 char mac[ADDR_LEN], ip[ADDR_LEN];
39 register int n, m;
40
41 if (!(eth_fp = fopen(ethers_name, "r"))) {
42 snprintf(s, 128, "fopen(%s):", ethers_name);
43 log_str(WARNING, s, strerror(errno));
44 return;
45 }
46
47 while (fgets(str, STR_SIZE, eth_fp)) {
48 if (debug > 2) fprintf(stderr, "ETHERS: %s", str);
49 for (n = 0; n < STR_SIZE; n++)
50 if ((str[n] == '\n') || (str[n] == '\r') || (str[n] == '#')) {
51 str[n] = '\0'; break;
52 }
53 for (mac[0] = ip[0] = '\0', n = 0; n < STR_SIZE; n++)
54 if ((str[n] != ' ') && (str[n] != '\t')) break;
55 if ((str[n] == '+') || (str[n] == '\0')) continue;
56 for (m = 0; (m < ADDR_LEN) && (n < STR_SIZE); m++, n++) {
57 if ((str[n] == ' ') || (str[n] == '\t') || (str[n] == '\0')) {
58 mac[m] = '\0'; break;
59 }
60 mac[m] = str[n];
61 }
62 for (; n < STR_SIZE; n++) if ((str[n] != ' ') && (str[n] != '\t')) break;
63 for (m = 0; (m < ADDR_LEN) && (n < STR_SIZE); m++, n++) {
64 if ((str[n] == ' ') || (str[n] == '\t') || (str[n] == '\0')) {
65 ip[m] = '\0'; break;
66 }
67 ip[m] = str[n];
68 }
69
70 if (ip[0] == '\0') { if (verbose) log_str(NOTICE, "Incorrect MAC-IP pair:", str); continue; }
71 if (strchr(mac, '.')) { strncpy(str, mac, ADDR_LEN); strncpy(mac, ip, ADDR_LEN); strncpy(ip, str, ADDR_LEN); }
72 if (!strchr(mac, ':')) { if (verbose) log_str(NOTICE, "Wrong MAC address", ip); continue; }
73 if (!strchr(ip, '.')) { if (verbose) log_str(NOTICE, "Wrong IP address", ip); continue; }
74
75 pair_add(mac, ip);
76 }
77
78 if (fclose(eth_fp)) {
79 snprintf(s, 128, "fclose(%s):", ethers_name);
80 log_str(ERROR, s, strerror(errno));
81 exit_ipguard(EXIT_FAILURE);
82 }
83 }
84
ethers_reinit(void)85 void ethers_reinit(void) {
86 pair_destroy(); pair_init(); ethers_init();
87 log_str(NOTICE, "MAC-IP file rescanned:", ethers_name);
88 }
89