]>
git.ipfire.org Git - ipfire-2.x.git/blob - src/misc-progs/rebuildhosts.c
1 /* IPCop helper program - rebuildhosts
3 * This program is distributed under the terms of the GNU General Public
4 * Licence. See the file COPYING for details.
6 * (c) Alan Hourihane, 2003
9 * $Id: rebuildhosts.c,v 1.3.2.6 2005/07/11 10:56:47 franck78 Exp $
13 #include "libsmooth.h"
19 #include <sys/types.h>
29 struct keyvalue
*kv
= NULL
;
31 void exithandler(void)
43 int main(int argc
, char *argv
[])
46 char hostname
[STRING_SIZE
] = "";
47 char domainname
[STRING_SIZE
] = "";
48 char gateway
[STRING_SIZE
] = "";
49 char buffer
[STRING_SIZE
];
50 char address
[STRING_SIZE
] = "";
51 char *active
, *ip
, *host
, *domain
;
59 memset(buffer
, 0, STRING_SIZE
);
62 if (!(readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings")))
64 fprintf(stderr
, "Couldn't read ethernet settings\n");
67 findkey(kv
, "GREEN_ADDRESS", address
);
71 if (!(readkeyvalues(kv
, CONFIG_ROOT
"/main/settings")))
73 fprintf(stderr
, "Couldn't read main settings\n");
76 strcpy(hostname
, SNAME
);
77 findkey(kv
, "HOSTNAME", hostname
);
78 findkey(kv
, "DOMAINNAME", domainname
);
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");
88 fprintf(stderr
, "Couldn't open remote-ipaddress file\n");
91 if (!(fd
= fopen(CONFIG_ROOT
"/main/hosts", "r")))
93 fprintf(stderr
, "Couldn't open main hosts file\n");
97 if (!(hosts
= fopen("/etc/hosts", "w")))
99 fprintf(stderr
, "Couldn't open /etc/hosts file\n");
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
);
108 fprintf(hosts
, "%s\t%s\n",address
,hostname
);
110 if (strlen(gateway
) > 0)
111 fprintf(hosts
, "%s\tgateway\n", gateway
);
113 while (fgets(buffer
, STRING_SIZE
, fd
))
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' */
120 ip
= strtok(NULL
, ",");
121 host
= strtok(NULL
, ",");
122 domain
= strtok(NULL
, ",");
125 continue; // bad line ? skip
129 fprintf(stderr
, "Bad IP: %s\n", ip
);
130 continue; /* bad ip, skip */
133 if (strspn(host
, LETTERS_NUMBERS
"-") != strlen(host
))
135 fprintf(stderr
, "Bad Host: %s\n", host
);
136 continue; /* bad name, skip */
140 fprintf(hosts
, "%s\t%s.%s\t%s\n",ip
,host
,domain
,host
);
142 fprintf(hosts
, "%s\t%s\n",ip
,host
);
149 if ((fdpid
= open("/var/run/dnsmasq.pid", O_RDONLY
)) == -1)
151 fprintf(stderr
, "Couldn't open pid file\n");
154 if (read(fdpid
, buffer
, STRING_SIZE
- 1) == -1)
156 fprintf(stderr
, "Couldn't read from pid file\n");
164 fprintf(stderr
, "Bad pid value\n");
167 if (kill(pid
, SIGHUP
) == -1)
169 fprintf(stderr
, "Unable to send SIGHUP\n");