File: dhcp6relay_script.c

package info (click to toggle)
wide-dhcpv6 20080615-22
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 4,616 kB
  • sloc: ansic: 14,877; yacc: 1,114; sh: 615; lex: 469; makefile: 119
file content (304 lines) | stat: -rw-r--r-- 7,528 bytes parent folder | download | duplicates (5)
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
/*from	$KAME: dhcp6c_script.c,v 1.11 2004/11/28 10:48:38 jinmei Exp $	*/

/*
 * Copyright (C) 2003 WIDE Project.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the project nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/queue.h>
#include <sys/wait.h>
#include <sys/stat.h>

#if TIME_WITH_SYS_TIME
# include <sys/time.h>
# include <time.h>
#else
# if HAVE_SYS_TIME_H
#  include <sys/time.h>
# else
#  include <time.h>
# endif
#endif

#include <netinet/in.h>

#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <syslog.h>
#include <errno.h>

#include "dhcp6.h"
#include "config.h"
#include "common.h"

static char client_str[] = "client";
static char buf[BUFSIZ];

static char *iapd2str __P((int, struct dhcp6_listval *));
static char *iana2str __P((int, struct dhcp6_listval *));

int
relay6_script(scriptpath, client, dh6, len)
	char *scriptpath;
	struct sockaddr_in6 *client;
	struct dhcp6 *dh6;
	int len;
{
	struct dhcp6_optinfo optinfo;
	struct dhcp6opt *optend;
	int i, j, iapds, ianas, envc, elen, ret = 0;
	char **envp, *s, *t;
	struct dhcp6_listval *v;
	pid_t pid, wpid;

	/* if a script is not specified, do nothing */
	if (scriptpath == NULL || strlen(scriptpath) == 0)
		return -1;

	/* only replies are interesting */
	if (dh6->dh6_msgtype != DH6_REPLY) {
		if (dh6->dh6_msgtype != DH6_ADVERTISE) {
			debug_printf(LOG_INFO, FNAME, "forward msg#%d to client?",
			    dh6->dh6_msgtype);
			return -1;
		}
		return 0;
	}

	/* parse options */
	optend = (struct dhcp6opt *)((caddr_t) dh6 + len);
	dhcp6_init_options(&optinfo);
	if (dhcp6_get_options((struct dhcp6opt *)(dh6 + 1), optend,
	    &optinfo) < 0) {
		debug_printf(LOG_INFO, FNAME, "failed to parse options");
		return -1;
	}

	/* initialize counters */
	iapds = 0;
	ianas = 0;
	envc = 2;     /* we at least include the address and the terminator */

	/* count the number of variables */
	for (v = TAILQ_FIRST(&optinfo.iapd_list); v; v = TAILQ_NEXT(v, link))
		iapds++;
	envc += iapds;
	for (v = TAILQ_FIRST(&optinfo.iana_list); v; v = TAILQ_NEXT(v, link))
		ianas++;
	envc += ianas;

	/* allocate an environments array */
	if ((envp = malloc(sizeof (char *) * envc)) == NULL) {
		debug_printf(LOG_NOTICE, FNAME,
		    "failed to allocate environment buffer");
		dhcp6_clear_options(&optinfo);
		return -1;
	}
	memset(envp, 0, sizeof (char *) * envc);

	/*
	 * Copy the parameters as environment variables
	 */
	i = 0;
	/* address */
	t = addr2str((struct sockaddr *) client);
	if (t == NULL) {
		debug_printf(LOG_NOTICE, FNAME,
		    "failed to get address of client");
		ret = -1;
		goto clean;
	}
	elen = sizeof (client_str) + 1 + strlen(t) + 1;
	if ((s = envp[i++] = malloc(elen)) == NULL) {
		debug_printf(LOG_NOTICE, FNAME,
		    "failed to allocate string for client");
		ret = -1;
		goto clean;
	}
	memset(s, 0, elen);
	snprintf(s, elen, "%s=%s", client_str, t);
	/* IAs */
	j = 0;
	for (v = TAILQ_FIRST(&optinfo.iapd_list); v;
	    v = TAILQ_NEXT(v, link)) {
		if ((s = envp[i++] = iapd2str(j++, v)) == NULL) {
			ret = -1;
			goto clean;
		}
	}
	j = 0;
	for (v = TAILQ_FIRST(&optinfo.iana_list); v;
	    v = TAILQ_NEXT(v, link)) {
		if ((s = envp[i++] = iana2str(j++, v)) == NULL) {
			ret = -1;
			goto clean;
		}
	}

	/* launch the script */
	pid = fork();
	if (pid < 0) {
		debug_printf(LOG_ERR, FNAME, "failed to fork: %s", strerror(errno));
		ret = -1;
		goto clean;
	} else if (pid) {
		int wstatus;

		do {
			wpid = wait(&wstatus);
		} while (wpid != pid && wpid > 0);

		if (wpid < 0)
			debug_printf(LOG_ERR, FNAME, "wait: %s", strerror(errno));
		else {
			debug_printf(LOG_DEBUG, FNAME,
			    "script \"%s\" terminated", scriptpath);
		}
	} else {
		char *argv[2];
		int fd;

		argv[0] = scriptpath;
		argv[1] = NULL;

		if (safefile(scriptpath)) {
			debug_printf(LOG_ERR, FNAME,
			    "script \"%s\" cannot be executed safely",
			    scriptpath);
			exit(1);
		}

		if (foreground == 0 &&
		    (fd = open("/dev/null", O_RDWR)) != -1) {
			dup2(fd, STDIN_FILENO);
			dup2(fd, STDOUT_FILENO);
			dup2(fd, STDERR_FILENO);
			if (fd > STDERR_FILENO)
				close(fd);
		}

		execve(scriptpath, argv, envp);

		debug_printf(LOG_ERR, FNAME, "child: exec failed: %s",
		    strerror(errno));
		exit(0);
	}

  clean:
	for (i = 0; i < envc; i++)
		free(envp[i]);
	free(envp);
	dhcp6_clear_options(&optinfo);

	return ret;
}

static char *
iapd2str(num, iav)
	int num;
	struct dhcp6_listval *iav;
{
	struct dhcp6_listval *siav;
	char *s, *r, *comma;

	s = buf;
	memset(s, 0, BUFSIZ);

	snprintf(s, BUFSIZ, "iapd_%d=", num);
	comma = "";

	for (siav = TAILQ_FIRST(&iav->sublist); siav;
	    siav = TAILQ_NEXT(siav, link)) {
		switch (siav->type) {
		case DHCP6_LISTVAL_PREFIX6:
			snprintf(s + strlen(s), BUFSIZ - strlen(s),
			    "%s%s/%d", comma,
			    in6addr2str(&siav->val_prefix6.addr, 0),
			    siav->val_prefix6.plen);
			comma = ",";
			break;

		case DHCP6_LISTVAL_STCODE:
			snprintf(s + strlen(s), BUFSIZ - strlen(s),
			    "%s#%d", comma, siav->val_num16);
			comma = ",";
			break;

		default:
			debug_printf(LOG_ERR, FNAME, "impossible subopt");
		}
	}

	if ((r = strdup(s)) == NULL)
		debug_printf(LOG_ERR, FNAME, "failed to allocate iapd_%d", num);
	return r;
}

static char *
iana2str(num, iav)
	int num;
	struct dhcp6_listval *iav;
{
	struct dhcp6_listval *siav;
	char *s, *r, *comma;

	s = buf;
	memset(s, 0, BUFSIZ);

	snprintf(s, BUFSIZ, "iana_%d=", num);
	comma = "";

	for (siav = TAILQ_FIRST(&iav->sublist); siav;
	    siav = TAILQ_NEXT(siav, link)) {
		switch (siav->type) {
		case DHCP6_LISTVAL_STATEFULADDR6:
			snprintf(s + strlen(s), BUFSIZ - strlen(s),
			    "%s%s", comma,
			    in6addr2str(&siav->val_statefuladdr6.addr, 0));
			comma = ",";
			break;

		case DHCP6_LISTVAL_STCODE:
			snprintf(s + strlen(s), BUFSIZ - strlen(s),
			    "%s#%d", comma, siav->val_num16);
			comma = ",";
			break;

		default:
			debug_printf(LOG_ERR, FNAME, "impossible subopt");
		}
	}

	if ((r = strdup(s)) == NULL)
		debug_printf(LOG_ERR, FNAME, "failed to allocate iana_%d", num);
	return r;
}