]> git.ipfire.org Git - people/teissler/ipfire-2.x.git/blame - src/misc-progs/rebuildhosts.c
tor: added daemon stats and little changes on styling
[people/teissler/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>
22#include "setuid.h"
23
24FILE *fd = NULL;
25FILE *hosts = NULL;
26FILE *gw = NULL;
27struct keyvalue *kv = NULL;
28
29void exithandler(void)
30{
31 if (kv)
32 freekeyvalues(kv);
33 if (fd)
34 fclose(fd);
35 if (hosts)
36 fclose(hosts);
37 if (gw)
38 fclose(gw);
39}
40
41int main(int argc, char *argv[])
42{
43 int fdpid;
95c26b24 44 char hostname[STRING_SIZE] = "";
584efcab
CS
45 char domainname[STRING_SIZE] = "";
46 char gateway[STRING_SIZE] = "";
47 char buffer[STRING_SIZE];
95c26b24 48 char address[STRING_SIZE] = "";
584efcab
CS
49 char *active, *ip, *host, *domain;
50 int pid;
51
52 if (!(initsetuid()))
53 exit(1);
54
55 atexit(exithandler);
56
57 memset(buffer, 0, STRING_SIZE);
58
59 kv = initkeyvalues();
60 if (!(readkeyvalues(kv, CONFIG_ROOT "/ethernet/settings")))
61 {
62 fprintf(stderr, "Couldn't read ethernet settings\n");
63 exit(1);
64 }
65 findkey(kv, "GREEN_ADDRESS", address);
66 freekeyvalues(kv);
67
68 kv = initkeyvalues();
69 if (!(readkeyvalues(kv, CONFIG_ROOT "/main/settings")))
70 {
71 fprintf(stderr, "Couldn't read main settings\n");
72 exit(1);
73 }
74 strcpy(hostname, SNAME );
75 findkey(kv, "HOSTNAME", hostname);
76 findkey(kv, "DOMAINNAME", domainname);
77 freekeyvalues(kv);
78 kv = NULL;
79
80 if (!(gw = fopen(CONFIG_ROOT "/red/remote-ipaddress", "r")))
81 {
82 fprintf(stderr, "Couldn't open remote-ipaddress file\n");
83 fclose(gw);
84 gw = NULL;
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}