File: hostinfo.c

package info (click to toggle)
dhcpcd 0.70-3
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 276 kB
  • ctags: 270
  • sloc: ansic: 2,300; sh: 391; makefile: 93
file content (362 lines) | stat: -rw-r--r-- 8,755 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/* $Id$
 *
 * dhcpcd - DHCP client daemon -
 * Copyright (C) 1996 - 1997 Yoichi Hariguchi <yoichi@fore.com>
 *
 * Dhcpcd is an RFC2131 and RFC1541 compliant DHCP client daemon.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/stat.h>
#include <net/if.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "dhcp.h"
#include "dhcp-options.h"
#include "error-handler.h"
#include "signal-handler.h"
#include "hostinfo.h"
#include "if.h"
#include "memory.h"

#define MAXLEN 256

extern char *CommandFile;


void
setupOptInfo(u_char *dest[], const u_char *src[])
{
	int i;

	for ( i = 0; i < MAXNOPT; ++i ) {
		if ( src[i] != NULL ) {
			if ( dest[i] != NULL ) {
				free(dest[i]);
			}
			dest[i] = smalloc(*src[i]+1);
			bcopy(src[i], dest[i], *src[i]+1);
		}
	}
}


void
freeOptInfo(u_char *optp[])
{
    int i;

	for ( i = 0; i < MAXNOPT; ++i ) {
		if ( optp[i] != NULL ) {
			free(optp[i]);
			optp[i] = NULL;
		}
	}
}

void
saveHostInfo(const u_char *optp[])
{
	int fd;
	char path[MAXLEN];
	char buf[MAXLEN];
	
	/* hostname
	 */
	if ( optp[OhostName] != NULL ) {
		strncpy(path, optp[OhostName]+1, *optp[OhostName]);
		if ( sethostname(path, *optp[OhostName]) < 0 ) {
			logSysRet("sethostname (setupHostInfo)");
		}
	}
	/* NIS domain name
	 */
	if ( optp[OnisDomName] != NULL ) {
		strncpy(path, optp[OnisDomName]+1, *optp[OnisDomName]);
		if ( setdomainname(path, *optp[OnisDomName]) < 0 ) {
			logSysRet("setdomainname (setupHostInfo)");
		}
	}
	/* default route (routers)
	 */
	if ( optp[Orouter] != NULL ) {
		setDefRoute(optp[Orouter], &Ifbuf);
	}
	/* make directory for resolv.conf and  hostinfo file
	 */
	if ( !setupHostInfoDir(HOST_INFO_DIR) ) {
		return;
	}
	/* ntp.conf
	 */
	if ( optp[OntpServer] != NULL ) {
		mkNTPconf(optp[OntpServer]);
	}
	/* resolv.conf
	 */
	if ( optp[Odns] != NULL && optp[OdomainName] != NULL ) {
		mkResolvConf(optp[Odns], optp[OdomainName]);
	}
	/* hostinfo file
	 */
	strncpy(path, HOST_INFO_DIR, MAXLEN);
	strncat(path, "/", MAXLEN);
	strncat(path, HOST_INFO_FILE, MAXLEN);
	strncat(path, "-", MAXLEN);
	strncat(path, Ifbuf.ifname, MAXLEN);
	if ( (fd = creat(path,  S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0 ) {
		logSysRet("creat (setupHostInfo)");
		return;
	}
      { char tmp[128];

      sprintf(tmp, "%ld", ntohl(LeaseTime));
      setenv("LEASETIME",tmp,1);
      sprintf(tmp, "%ld", ntohl(RenewTime));
      setenv("RENEWALTIME",tmp,1);
      sprintf(tmp, "%ld", ntohl(RebindTime));
      setenv("REBINDTIME",tmp,1);
      setenv("IPADDR",inet_ntoa(*((struct in_addr *)&Ifbuf.addr)),1);
      setenv("NETMASK",inet_ntoa(*((struct in_addr *)&Ifbuf.mask)),1);
      setenv("BROADCAST",inet_ntoa(*((struct in_addr *)&Ifbuf.bcast)),1);
      }


	sprintf(buf, "LEASETIME=%ld\n", ntohl(LeaseTime));
	sprintf(buf+strlen(buf), "RENEWALTIME=%ld\n", ntohl(RenewTime));
	sprintf(buf+strlen(buf), "REBINDTIME=%ld\n", ntohl(RebindTime));
	sprintf(buf+strlen(buf), "IPADDR=%s\n",
			inet_ntoa(*((struct in_addr *)&Ifbuf.addr)));
	sprintf(buf+strlen(buf), "NETMASK=%s\n",
			inet_ntoa(*((struct in_addr *)&Ifbuf.mask)));
	sprintf(buf+strlen(buf), "BROADCAST=%s\n",
			inet_ntoa(*((struct in_addr *)&Ifbuf.bcast)));
	if ( write(fd, buf, strlen(buf)) < 0 ) {
		logSysRet("write (setupHostInfo)");
	}
	if ( optp[OhostName] != NULL ) {
		addHostInfo(fd, OT_STRING, "HOSTNAME", optp[OhostName]);
	}
	if ( optp[OnisDomName] != NULL ) {
		addHostInfo(fd, OT_STRING, "NISDOMAINNAME", optp[OnisDomName]);
	}
	if ( optp[OlprServer] != NULL ) {
		addHostInfo(fd, OT_ADDR, "LPRSERVER", optp[OlprServer]);
	}
	if ( optp[OntpServer] != NULL ) {
		addHostInfo(fd, OT_ADDR, "NTPSERVER", optp[OntpServer]);
	}
	if ( optp[OtimeServer] != NULL ) {
		addHostInfo(fd, OT_ADDR, "TIMESERVR", optp[OtimeServer]);
	}
	if ( optp[Orouter] != NULL ) {
		addHostInfo(fd, OT_ADDR, "ROUTER", optp[Orouter]);
	}
	close(fd);
}


int
setupHostInfoDir(const char *dir)
{
	struct stat stbuf;

	if ( stat(dir, &stbuf) < 0 ) {
		if ( errno == ENOENT ) {
			if ( mkdir(dir, S_IRUSR | S_IWUSR | S_IXUSR |
					   S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0 ) {
				logSysRet("mkdir (setupHostInfoDir)");
				return 0;
			}
			return 1;
		} else {
		  logSysRet("stat (setupHostInfoDir)");
		  return 0;
		}
	}
	if ( !S_ISDIR(stbuf.st_mode) ) {
		chmod(dir, S_IRUSR | S_IWUSR);
		if ( unlink(dir) < 0 ) {
			logSysRet("unlink (setupHostInfoDir)");
			return 0;
		}
		if ( mkdir(dir, S_IRUSR | S_IWUSR | S_IXUSR |
				   S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0 ) {
			logSysRet("mkdir (setupHostInfoDir)");
			return 0;
		}
	}
	if ( chmod(dir, S_IRUSR | S_IWUSR | S_IXUSR |
			   S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0 ) {
		logSysRet("chmod (setupHostInfoDir)");
		return 0;
	}
	return 1;
}

void
addHostInfo(int fd, const int flag, const char *name, const u_char *optp)
{
	char	buf[MAXLEN];
	char	env[MAXLEN];
	int		i;
	u_int  *p;

	switch ( flag ) {
	  case OT_STRING:
		strncpy(env, optp+1, *optp);
		if ( setenv(name, env, 1) < 0 ) {
			logRet("setenv (addHostInfo): insufficient space");
		}
		strcpy(buf, name);
		strcat(buf, "=");
		strncat(buf, optp+1, *optp);
		strcat(buf, "\n");
		break;
	  case OT_ADDR:
		p = (u_int *)(optp + 1);
		if ( setenv(name, inet_ntoa(*((struct in_addr *)p)), 1) < 0 ) {
			logRet("setenv (addHostInfo): insufficient space");
		}
		strcpy(buf, name);
		strcat(buf, "=");
		strcat(buf, inet_ntoa(*((struct in_addr *)p)));
		++p;
		strcat(buf, "\n");
		for ( i = 1; i < *optp/4; ++i ) {
			sprintf(env, "%s%d", name, i+1);
			if ( setenv(env, inet_ntoa(*((struct in_addr *)p)), 1) < 0 ) {
				logRet("setenv (addHostInfo): insufficient space");
			}
			sprintf(buf+strlen(buf), "%s%d=%s\n", name, i+1,
					inet_ntoa(*((struct in_addr *)p)));
			++p;
		}
		break;
	  default:
		return;
	}
	if ( write(fd, buf, strlen(buf)) < 0 ) {
		logSysRet("write (addHostInfo)");
	}
}

void
mkNTPconf(const u_char *addr)
{
	char buf[MAXLEN];
	int  fd;
	int  i;
	u_int *p;

	strcpy(buf, HOST_INFO_DIR);
	strcat(buf, "/");
	strcat(buf, "ntp.conf");
	if ( (fd = creat(buf,  S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0 ) {
		logSysRet("creat (mkNTPconf)");
		return;
	}
	/* TODO: check whether NTP service is working on those addresses
	 */
	p = (u_int *)(addr + 1);
	*buf = '\0';
	for ( i = 0; i < *addr/4; ++i ) {
		sprintf(buf+strlen(buf), "server %s\n",
				inet_ntoa(*((struct in_addr *)p)));
		++p;
	}
	if ( write(fd, buf, strlen(buf)) < 0 ) {
		logSysRet("write (mkNTPconf)");
	}
	close(fd);
}


void
mkResolvConf(const u_char *addr, const u_char *domName)
{
	char buf[MAXLEN];
	int  fd;
	int  i;
	u_int *p;

	strcpy(buf, HOST_INFO_DIR);
	strcat(buf, "/");
	strcat(buf, "resolv.conf");
	if ( (fd = creat(buf,  S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0 ) {
		logSysRet("creat (mkResolvConf)");
		return;
	}
	/* TODO: check whether name server is working on those addresses
	 */
	strcpy(buf, "domain ");
	strncat(buf, domName+1, *domName);
	strcat(buf, "\n");
	p = (u_int *)(addr + 1);
	for ( i = 0; i < *addr/4; ++i ) {
		sprintf(buf+strlen(buf), "nameserver %s\n",
				inet_ntoa(*((struct in_addr *)p)));
		++p;
	}
	if ( write(fd, buf, strlen(buf)) < 0 ) {
		logSysRet("write (mkResolvConf)");
	}
	close(fd);
}

void
execCommandFile()
{
	pid_t pid;

	if ( CommandFile == NULL ) {
		return;
	}
	if ( (pid = fork()) < 0 ) {
		logSysRet("first fork (execCommandFile)");
	} else if ( pid == 0 ) {
              close(Srecv);
              close(Ssend);
		if ( (pid = fork()) < 0 ) {
			logSysRet("second fork (execCommandFile)");
		} else if ( pid > 0 ) {
			/* we're the 1st child. let's just exit
			 */
			exit(0);
		}
		/* we are the second child. let's exec the command file
		 */
		if ( execlp(CommandFile, CommandFile, NULL) < 0 ) {
			logSysRet("execlp (execCommandFile)");
		}
	}
	/* we're the parent (original process).
	 * let's wait for the first child
	 */
	if ( waitpid(pid, NULL, 0) != pid ) {
		logSysRet("waitpid (execCommandFile)");
	}
}