]>
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>
26 struct keyvalue
*kv
= NULL
;
28 void exithandler(void)
38 int main(int argc
, char *argv
[])
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
;
53 memset(buffer
, 0, STRING_SIZE
);
56 if (!(readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings")))
58 fprintf(stderr
, "Couldn't read ethernet settings\n");
61 findkey(kv
, "GREEN_ADDRESS", address
);
65 if (!(readkeyvalues(kv
, CONFIG_ROOT
"/main/settings")))
67 fprintf(stderr
, "Couldn't read main settings\n");
70 strcpy(hostname
, SNAME
);
71 findkey(kv
, "HOSTNAME", hostname
);
72 findkey(kv
, "DOMAINNAME", domainname
);
76 if (!(fd
= fopen(CONFIG_ROOT
"/main/hosts", "r")))
78 fprintf(stderr
, "Couldn't open main hosts file\n");
81 if (!(hosts
= fopen("/etc/hosts", "w")))
83 fprintf(stderr
, "Couldn't open /etc/hosts file\n");
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
);
92 fprintf(hosts
, "%s\t%s\n",address
,hostname
);
93 while (fgets(buffer
, STRING_SIZE
, fd
))
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' */
100 ip
= strtok(NULL
, ",");
101 host
= strtok(NULL
, ",");
102 domain
= strtok(NULL
, ",");
105 continue; // bad line ? skip
109 fprintf(stderr
, "Bad IP: %s\n", ip
);
110 continue; /* bad ip, skip */
113 if (strspn(host
, LETTERS_NUMBERS
"-") != strlen(host
))
115 fprintf(stderr
, "Bad Host: %s\n", host
);
116 continue; /* bad name, skip */
120 fprintf(hosts
, "%s\t%s.%s\t%s\n",ip
,host
,domain
,host
);
122 fprintf(hosts
, "%s\t%s\n",ip
,host
);
129 if ((fdpid
= open("/var/run/dnsmasq.pid", O_RDONLY
)) == -1)
131 fprintf(stderr
, "Couldn't open pid file\n");
134 if (read(fdpid
, buffer
, STRING_SIZE
- 1) == -1)
136 fprintf(stderr
, "Couldn't read from pid file\n");
144 fprintf(stderr
, "Bad pid value\n");
147 if (kill(pid
, SIGHUP
) == -1)
149 fprintf(stderr
, "Unable to send SIGHUP\n");