]>
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>
27 struct keyvalue
*kv
= NULL
;
29 void exithandler(void)
41 int main(int argc
, char *argv
[])
44 char hostname
[STRING_SIZE
];
45 char domainname
[STRING_SIZE
] = "";
46 char gateway
[STRING_SIZE
] = "";
47 char buffer
[STRING_SIZE
];
48 char address
[STRING_SIZE
];
49 char *active
, *ip
, *host
, *domain
;
57 memset(buffer
, 0, STRING_SIZE
);
60 if (!(readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings")))
62 fprintf(stderr
, "Couldn't read ethernet settings\n");
65 findkey(kv
, "GREEN_ADDRESS", address
);
69 if (!(readkeyvalues(kv
, CONFIG_ROOT
"/main/settings")))
71 fprintf(stderr
, "Couldn't read main settings\n");
74 strcpy(hostname
, SNAME
);
75 findkey(kv
, "HOSTNAME", hostname
);
76 findkey(kv
, "DOMAINNAME", domainname
);
80 if (!(gw
= fopen(CONFIG_ROOT
"/red/remote-ipaddress", "r")))
82 fprintf(stderr
, "Couldn't open remote-ipaddress file\n");
88 if (fgets(gateway
, STRING_SIZE
, gw
) == NULL
)
90 fprintf(stderr
, "Couldn't read remote-ipaddress\n");
94 if (!(fd
= fopen(CONFIG_ROOT
"/main/hosts", "r")))
96 fprintf(stderr
, "Couldn't open main hosts file\n");
100 if (!(hosts
= fopen("/etc/hosts", "w")))
102 fprintf(stderr
, "Couldn't open /etc/hosts file\n");
107 fprintf(hosts
, "127.0.0.1\tlocalhost\n");
108 if (strlen(domainname
))
109 fprintf(hosts
, "%s\t%s.%s\t%s\n",address
,hostname
,domainname
,hostname
);
111 fprintf(hosts
, "%s\t%s\n",address
,hostname
);
113 fprintf(hosts
, "%s\tgateway\n",gateway
);
115 while (fgets(buffer
, STRING_SIZE
, fd
))
117 buffer
[strlen(buffer
) - 1] = 0;
118 if (buffer
[0]==',') continue; /* disabled if empty field */
119 active
= strtok(buffer
, ",");
120 if (strcmp(active
, "off")==0) continue; /* or 'off' */
122 ip
= strtok(NULL
, ",");
123 host
= strtok(NULL
, ",");
124 domain
= strtok(NULL
, ",");
127 continue; // bad line ? skip
131 fprintf(stderr
, "Bad IP: %s\n", ip
);
132 continue; /* bad ip, skip */
135 if (strspn(host
, LETTERS_NUMBERS
"-") != strlen(host
))
137 fprintf(stderr
, "Bad Host: %s\n", host
);
138 continue; /* bad name, skip */
142 fprintf(hosts
, "%s\t%s.%s\t%s\n",ip
,host
,domain
,host
);
144 fprintf(hosts
, "%s\t%s\n",ip
,host
);
151 if ((fdpid
= open("/var/run/dnsmasq.pid", O_RDONLY
)) == -1)
153 fprintf(stderr
, "Couldn't open pid file\n");
156 if (read(fdpid
, buffer
, STRING_SIZE
- 1) == -1)
158 fprintf(stderr
, "Couldn't read from pid file\n");
166 fprintf(stderr
, "Bad pid value\n");
169 if (kill(pid
, SIGHUP
) == -1)
171 fprintf(stderr
, "Unable to send SIGHUP\n");