]> git.ipfire.org Git - ipfire-2.x.git/blob - src/misc-progs/rebuildhosts.c
Remove orphaned Apache patch
[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 <string.h>
18 #include <sys/types.h>
19 #include <sys/stat.h>
20
21 #include "setuid.h"
22 #include "netutil.h"
23
24 FILE *hosts = NULL;
25 FILE *gw = NULL;
26 struct keyvalue *kv = NULL;
27
28 void exithandler(void)
29 {
30 if (kv)
31 freekeyvalues(kv);
32 if (hosts)
33 fclose(hosts);
34 if (gw)
35 fclose(gw);
36 }
37
38 int main(int argc, char *argv[])
39 {
40 char hostname[STRING_SIZE] = "";
41 char domainname[STRING_SIZE] = "";
42 char gateway[STRING_SIZE] = "";
43 char address[STRING_SIZE] = "";
44
45 if (!(initsetuid()))
46 exit(1);
47
48 atexit(exithandler);
49
50 kv = initkeyvalues();
51 if (!(readkeyvalues(kv, CONFIG_ROOT "/ethernet/settings")))
52 {
53 fprintf(stderr, "Couldn't read ethernet settings\n");
54 exit(1);
55 }
56 findkey(kv, "GREEN_ADDRESS", address);
57 freekeyvalues(kv);
58
59 kv = initkeyvalues();
60 if (!(readkeyvalues(kv, CONFIG_ROOT "/main/settings")))
61 {
62 fprintf(stderr, "Couldn't read main settings\n");
63 exit(1);
64 }
65 strcpy(hostname, SNAME );
66 findkey(kv, "HOSTNAME", hostname);
67 findkey(kv, "DOMAINNAME", domainname);
68 freekeyvalues(kv);
69 kv = NULL;
70
71 if ((gw = fopen(CONFIG_ROOT "/red/remote-ipaddress", "r"))) {
72 if (fgets(gateway, STRING_SIZE, gw) == NULL) {
73 fprintf(stderr, "Couldn't read remote-ipaddress\n");
74 exit(1);
75 }
76 } else {
77 fprintf(stderr, "Couldn't open remote-ipaddress file\n");
78 }
79
80 if (!(hosts = fopen("/etc/hosts", "w")))
81 {
82 fprintf(stderr, "Couldn't open /etc/hosts file\n");
83 exit(1);
84 }
85 fprintf(hosts, "127.0.0.1\tlocalhost\n");
86 if (strlen(domainname))
87 fprintf(hosts, "%s\t%s.%s\t%s\n",address,hostname,domainname,hostname);
88 else
89 fprintf(hosts, "%s\t%s\n",address,hostname);
90
91 if (strlen(gateway) > 0)
92 fprintf(hosts, "%s\tgateway\n", gateway);
93
94 return 0;
95 }