]> git.ipfire.org Git - ipfire-2.x.git/blame - src/misc-progs/rebuildhosts.c
correct wrong headline at hardwaregraphs.cgi
[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>
584efcab
CS
17#include <string.h>
18#include <sys/types.h>
19#include <sys/stat.h>
52e54c1c 20
584efcab 21#include "setuid.h"
52e54c1c 22#include "netutil.h"
584efcab 23
584efcab
CS
24FILE *hosts = NULL;
25FILE *gw = NULL;
26struct keyvalue *kv = NULL;
27
28void exithandler(void)
29{
30 if (kv)
31 freekeyvalues(kv);
584efcab
CS
32 if (hosts)
33 fclose(hosts);
34 if (gw)
35 fclose(gw);
36}
37
38int main(int argc, char *argv[])
39{
95c26b24 40 char hostname[STRING_SIZE] = "";
584efcab
CS
41 char domainname[STRING_SIZE] = "";
42 char gateway[STRING_SIZE] = "";
95c26b24 43 char address[STRING_SIZE] = "";
584efcab
CS
44
45 if (!(initsetuid()))
46 exit(1);
47
48 atexit(exithandler);
49
584efcab
CS
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
e528fb2c
MT
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 {
584efcab 77 fprintf(stderr, "Couldn't open remote-ipaddress file\n");
584efcab
CS
78 }
79
584efcab
CS
80 if (!(hosts = fopen("/etc/hosts", "w")))
81 {
82 fprintf(stderr, "Couldn't open /etc/hosts file\n");
584efcab
CS
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
e528fb2c
MT
91 if (strlen(gateway) > 0)
92 fprintf(hosts, "%s\tgateway\n", gateway);
584efcab 93
584efcab
CS
94 return 0;
95}