fdaeed618ee446ad11ad9246c7d6deb957494203
[ipfire-2.x.git] / src / misc-progs / rebuildhosts.c
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>
22
23 #include "setuid.h"
24 #include "netutil.h"
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;
46 char hostname[STRING_SIZE] = "";
47 char domainname[STRING_SIZE] = "";
48 char gateway[STRING_SIZE] = "";
49 char buffer[STRING_SIZE];
50 char address[STRING_SIZE] = "";
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 exit(1);
86 }
87
88 if (fgets(gateway, STRING_SIZE, gw) == NULL)
89 {
90 fprintf(stderr, "Couldn't read remote-ipaddress\n");
91 exit(1);
92 }
93
94 if (!(fd = fopen(CONFIG_ROOT "/main/hosts", "r")))
95 {
96 fprintf(stderr, "Couldn't open main hosts file\n");
97 exit(1);
98 }
99
100 if (!(hosts = fopen("/etc/hosts", "w")))
101 {
102 fprintf(stderr, "Couldn't open /etc/hosts file\n");
103 fclose(fd);
104 fd = NULL;
105 exit(1);
106 }
107 fprintf(hosts, "127.0.0.1\tlocalhost\n");
108 if (strlen(domainname))
109 fprintf(hosts, "%s\t%s.%s\t%s\n",address,hostname,domainname,hostname);
110 else
111 fprintf(hosts, "%s\t%s\n",address,hostname);
112
113 fprintf(hosts, "%s\tgateway\n",gateway);
114
115 while (fgets(buffer, STRING_SIZE, fd))
116 {
117 buffer[strlen(buffer) - 1] = 0;
118 if (buffer[0]==',') continue; /* disabled if empty field */
119 active = strtok(buffer, ",");
120 if (strcmp(active, "off")==0) continue; /* or 'off' */
121
122 ip = strtok(NULL, ",");
123 host = strtok(NULL, ",");
124 domain = strtok(NULL, ",");
125
126 if (!(ip && host))
127 continue; // bad line ? skip
128
129 if (!VALID_IP(ip))
130 {
131 fprintf(stderr, "Bad IP: %s\n", ip);
132 continue; /* bad ip, skip */
133 }
134
135 if (strspn(host, LETTERS_NUMBERS "-") != strlen(host))
136 {
137 fprintf(stderr, "Bad Host: %s\n", host);
138 continue; /* bad name, skip */
139 }
140
141 if (domain)
142 fprintf(hosts, "%s\t%s.%s\t%s\n",ip,host,domain,host);
143 else
144 fprintf(hosts, "%s\t%s\n",ip,host);
145 }
146 fclose(fd);
147 fd = NULL;
148 fclose(hosts);
149 hosts = NULL;
150
151 if ((fdpid = open("/var/run/dnsmasq.pid", O_RDONLY)) == -1)
152 {
153 fprintf(stderr, "Couldn't open pid file\n");
154 exit(1);
155 }
156 if (read(fdpid, buffer, STRING_SIZE - 1) == -1)
157 {
158 fprintf(stderr, "Couldn't read from pid file\n");
159 close(fdpid);
160 exit(1);
161 }
162 close(fdpid);
163 pid = atoi(buffer);
164 if (pid <= 1)
165 {
166 fprintf(stderr, "Bad pid value\n");
167 exit(1);
168 }
169 if (kill(pid, SIGHUP) == -1)
170 {
171 fprintf(stderr, "Unable to send SIGHUP\n");
172 exit(1);
173 }
174
175 return 0;
176 }