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