]>
Commit | Line | Data |
---|---|---|
584efcab CS |
1 | /* IPCop helper program - rebuildhosts |
2 | * | |
3 | * This program is distributed under the terms of the GNU General Public | |
4 | * Licence. See the file COPYING for details. | |
5 | * | |
6 | * (c) Alan Hourihane, 2003 | |
7 | * | |
8 | * | |
9 | * $Id: rebuildhosts.c,v 1.3.2.6 2005/07/11 10:56:47 franck78 Exp $ | |
10 | * | |
11 | */ | |
12 | ||
13 | #include "libsmooth.h" | |
14 | #include <stdio.h> | |
15 | #include <stdlib.h> | |
16 | #include <unistd.h> | |
17 | #include <fcntl.h> | |
18 | #include <string.h> | |
19 | #include <sys/types.h> | |
20 | #include <sys/stat.h> | |
21 | #include <signal.h> | |
52e54c1c | 22 | |
584efcab | 23 | #include "setuid.h" |
52e54c1c | 24 | #include "netutil.h" |
584efcab CS |
25 | |
26 | FILE *fd = NULL; | |
27 | FILE *hosts = NULL; | |
28 | FILE *gw = NULL; | |
29 | struct keyvalue *kv = NULL; | |
30 | ||
31 | void exithandler(void) | |
32 | { | |
33 | if (kv) | |
34 | freekeyvalues(kv); | |
35 | if (fd) | |
36 | fclose(fd); | |
37 | if (hosts) | |
38 | fclose(hosts); | |
39 | if (gw) | |
40 | fclose(gw); | |
41 | } | |
42 | ||
43 | int main(int argc, char *argv[]) | |
44 | { | |
45 | int fdpid; | |
95c26b24 | 46 | char hostname[STRING_SIZE] = ""; |
584efcab CS |
47 | char domainname[STRING_SIZE] = ""; |
48 | char gateway[STRING_SIZE] = ""; | |
49 | char buffer[STRING_SIZE]; | |
95c26b24 | 50 | char address[STRING_SIZE] = ""; |
584efcab CS |
51 | char *active, *ip, *host, *domain; |
52 | int pid; | |
53 | ||
54 | if (!(initsetuid())) | |
55 | exit(1); | |
56 | ||
57 | atexit(exithandler); | |
58 | ||
59 | memset(buffer, 0, STRING_SIZE); | |
60 | ||
61 | kv = initkeyvalues(); | |
62 | if (!(readkeyvalues(kv, CONFIG_ROOT "/ethernet/settings"))) | |
63 | { | |
64 | fprintf(stderr, "Couldn't read ethernet settings\n"); | |
65 | exit(1); | |
66 | } | |
67 | findkey(kv, "GREEN_ADDRESS", address); | |
68 | freekeyvalues(kv); | |
69 | ||
70 | kv = initkeyvalues(); | |
71 | if (!(readkeyvalues(kv, CONFIG_ROOT "/main/settings"))) | |
72 | { | |
73 | fprintf(stderr, "Couldn't read main settings\n"); | |
74 | exit(1); | |
75 | } | |
76 | strcpy(hostname, SNAME ); | |
77 | findkey(kv, "HOSTNAME", hostname); | |
78 | findkey(kv, "DOMAINNAME", domainname); | |
79 | freekeyvalues(kv); | |
80 | kv = NULL; | |
81 | ||
82 | if (!(gw = fopen(CONFIG_ROOT "/red/remote-ipaddress", "r"))) | |
83 | { | |
84 | fprintf(stderr, "Couldn't open remote-ipaddress file\n"); | |
85 | fclose(gw); | |
86 | gw = NULL; | |
87 | exit(1); | |
88 | } | |
89 | ||
90 | if (fgets(gateway, STRING_SIZE, gw) == NULL) | |
91 | { | |
92 | fprintf(stderr, "Couldn't read remote-ipaddress\n"); | |
93 | exit(1); | |
94 | } | |
95 | ||
96 | if (!(fd = fopen(CONFIG_ROOT "/main/hosts", "r"))) | |
97 | { | |
98 | fprintf(stderr, "Couldn't open main hosts file\n"); | |
99 | exit(1); | |
100 | } | |
101 | ||
102 | if (!(hosts = fopen("/etc/hosts", "w"))) | |
103 | { | |
104 | fprintf(stderr, "Couldn't open /etc/hosts file\n"); | |
105 | fclose(fd); | |
106 | fd = NULL; | |
107 | exit(1); | |
108 | } | |
109 | fprintf(hosts, "127.0.0.1\tlocalhost\n"); | |
110 | if (strlen(domainname)) | |
111 | fprintf(hosts, "%s\t%s.%s\t%s\n",address,hostname,domainname,hostname); | |
112 | else | |
113 | fprintf(hosts, "%s\t%s\n",address,hostname); | |
114 | ||
115 | fprintf(hosts, "%s\tgateway\n",gateway); | |
116 | ||
117 | while (fgets(buffer, STRING_SIZE, fd)) | |
118 | { | |
119 | buffer[strlen(buffer) - 1] = 0; | |
120 | if (buffer[0]==',') continue; /* disabled if empty field */ | |
121 | active = strtok(buffer, ","); | |
122 | if (strcmp(active, "off")==0) continue; /* or 'off' */ | |
123 | ||
124 | ip = strtok(NULL, ","); | |
125 | host = strtok(NULL, ","); | |
126 | domain = strtok(NULL, ","); | |
127 | ||
128 | if (!(ip && host)) | |
129 | continue; // bad line ? skip | |
130 | ||
131 | if (!VALID_IP(ip)) | |
132 | { | |
133 | fprintf(stderr, "Bad IP: %s\n", ip); | |
134 | continue; /* bad ip, skip */ | |
135 | } | |
136 | ||
137 | if (strspn(host, LETTERS_NUMBERS "-") != strlen(host)) | |
138 | { | |
139 | fprintf(stderr, "Bad Host: %s\n", host); | |
140 | continue; /* bad name, skip */ | |
141 | } | |
142 | ||
143 | if (domain) | |
144 | fprintf(hosts, "%s\t%s.%s\t%s\n",ip,host,domain,host); | |
145 | else | |
146 | fprintf(hosts, "%s\t%s\n",ip,host); | |
147 | } | |
148 | fclose(fd); | |
149 | fd = NULL; | |
150 | fclose(hosts); | |
151 | hosts = NULL; | |
152 | ||
153 | if ((fdpid = open("/var/run/dnsmasq.pid", O_RDONLY)) == -1) | |
154 | { | |
155 | fprintf(stderr, "Couldn't open pid file\n"); | |
156 | exit(1); | |
157 | } | |
158 | if (read(fdpid, buffer, STRING_SIZE - 1) == -1) | |
159 | { | |
160 | fprintf(stderr, "Couldn't read from pid file\n"); | |
161 | close(fdpid); | |
162 | exit(1); | |
163 | } | |
164 | close(fdpid); | |
165 | pid = atoi(buffer); | |
166 | if (pid <= 1) | |
167 | { | |
168 | fprintf(stderr, "Bad pid value\n"); | |
169 | exit(1); | |
170 | } | |
171 | if (kill(pid, SIGHUP) == -1) | |
172 | { | |
173 | fprintf(stderr, "Unable to send SIGHUP\n"); | |
174 | exit(1); | |
175 | } | |
176 | ||
177 | return 0; | |
178 | } |