]> git.ipfire.org Git - ipfire-2.x.git/blob - src/misc-progs/rebuildhosts.c
Connections.cgi neu geschrieben.
[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 #include "setuid.h"
23
24 FILE *fd = NULL;
25 FILE *hosts = NULL;
26 struct keyvalue *kv = NULL;
27
28 void exithandler(void)
29 {
30 if (kv)
31 freekeyvalues(kv);
32 if (fd)
33 fclose(fd);
34 if (hosts)
35 fclose(hosts);
36 }
37
38 int main(int argc, char *argv[])
39 {
40 int fdpid;
41 char hostname[STRING_SIZE];
42 char domainname[STRING_SIZE] = "";
43 char buffer[STRING_SIZE];
44 char address[STRING_SIZE];
45 char *active, *ip, *host, *domain;
46 int pid;
47
48 if (!(initsetuid()))
49 exit(1);
50
51 atexit(exithandler);
52
53 memset(buffer, 0, STRING_SIZE);
54
55 kv = initkeyvalues();
56 if (!(readkeyvalues(kv, CONFIG_ROOT "/ethernet/settings")))
57 {
58 fprintf(stderr, "Couldn't read ethernet settings\n");
59 exit(1);
60 }
61 findkey(kv, "GREEN_ADDRESS", address);
62 freekeyvalues(kv);
63
64 kv = initkeyvalues();
65 if (!(readkeyvalues(kv, CONFIG_ROOT "/main/settings")))
66 {
67 fprintf(stderr, "Couldn't read main settings\n");
68 exit(1);
69 }
70 strcpy(hostname, SNAME );
71 findkey(kv, "HOSTNAME", hostname);
72 findkey(kv, "DOMAINNAME", domainname);
73 freekeyvalues(kv);
74 kv = NULL;
75
76 if (!(fd = fopen(CONFIG_ROOT "/main/hosts", "r")))
77 {
78 fprintf(stderr, "Couldn't open main hosts file\n");
79 exit(1);
80 }
81 if (!(hosts = fopen("/etc/hosts", "w")))
82 {
83 fprintf(stderr, "Couldn't open /etc/hosts file\n");
84 fclose(fd);
85 fd = NULL;
86 exit(1);
87 }
88 fprintf(hosts, "127.0.0.1\tlocalhost\n");
89 if (strlen(domainname))
90 fprintf(hosts, "%s\t%s.%s\t%s\n",address,hostname,domainname,hostname);
91 else
92 fprintf(hosts, "%s\t%s\n",address,hostname);
93 while (fgets(buffer, STRING_SIZE, fd))
94 {
95 buffer[strlen(buffer) - 1] = 0;
96 if (buffer[0]==',') continue; /* disabled if empty field */
97 active = strtok(buffer, ",");
98 if (strcmp(active, "off")==0) continue; /* or 'off' */
99
100 ip = strtok(NULL, ",");
101 host = strtok(NULL, ",");
102 domain = strtok(NULL, ",");
103
104 if (!(ip && host))
105 continue; // bad line ? skip
106
107 if (!VALID_IP(ip))
108 {
109 fprintf(stderr, "Bad IP: %s\n", ip);
110 continue; /* bad ip, skip */
111 }
112
113 if (strspn(host, LETTERS_NUMBERS "-") != strlen(host))
114 {
115 fprintf(stderr, "Bad Host: %s\n", host);
116 continue; /* bad name, skip */
117 }
118
119 if (domain)
120 fprintf(hosts, "%s\t%s.%s\t%s\n",ip,host,domain,host);
121 else
122 fprintf(hosts, "%s\t%s\n",ip,host);
123 }
124 fclose(fd);
125 fd = NULL;
126 fclose(hosts);
127 hosts = NULL;
128
129 if ((fdpid = open("/var/run/dnsmasq.pid", O_RDONLY)) == -1)
130 {
131 fprintf(stderr, "Couldn't open pid file\n");
132 exit(1);
133 }
134 if (read(fdpid, buffer, STRING_SIZE - 1) == -1)
135 {
136 fprintf(stderr, "Couldn't read from pid file\n");
137 close(fdpid);
138 exit(1);
139 }
140 close(fdpid);
141 pid = atoi(buffer);
142 if (pid <= 1)
143 {
144 fprintf(stderr, "Bad pid value\n");
145 exit(1);
146 }
147 if (kill(pid, SIGHUP) == -1)
148 {
149 fprintf(stderr, "Unable to send SIGHUP\n");
150 exit(1);
151 }
152
153 return 0;
154 }