1/*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2014 The FreeBSD Foundation
5 *
6 * This software was developed by Edward Tomasz Napierala under sponsorship
7 * from the FreeBSD Foundation.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 *
30 */
31
32/*
33 * All the "defined" stuff is for handling variables,
34 * such as ${OSNAME}, in maps.
35 */
36
37#include <sys/types.h>
38#include <sys/time.h>
39#include <sys/ioctl.h>
40#include <sys/param.h>
41#include <sys/linker.h>
42#include <sys/mount.h>
43#include <sys/socket.h>
44#include <sys/stat.h>
45#include <sys/wait.h>
46#include <sys/utsname.h>
47#include <assert.h>
48#include <ctype.h>
49#include <errno.h>
50#include <fcntl.h>
51#include <libgen.h>
52#include <libutil.h>
53#include <netdb.h>
54#include <signal.h>
55#include <stdbool.h>
56#include <stdint.h>
57#include <stdio.h>
58#include <stdlib.h>
59#include <string.h>
60#include <unistd.h>
61
62#include "common.h"
63
64static TAILQ_HEAD(, defined_value)	defined_values;
65
66static const char *
67defined_find(const char *name)
68{
69	struct defined_value *d;
70
71	TAILQ_FOREACH(d, &defined_values, d_next) {
72		if (strcmp(d->d_name, name) == 0)
73			return (d->d_value);
74	}
75
76	return (NULL);
77}
78
79char *
80defined_expand(const char *string)
81{
82	const char *value;
83	char c, *expanded, *name;
84	int i, ret, before_len = 0, name_off = 0, name_len = 0, after_off = 0;
85	bool backslashed = false, bracketed = false;
86
87	expanded = checked_strdup(string);
88
89	for (i = 0; string[i] != '\0'; i++) {
90		c = string[i];
91		if (c == '\\' && backslashed == false) {
92			backslashed = true;
93			continue;
94		}
95		if (backslashed) {
96			backslashed = false;
97			continue;
98		}
99		backslashed = false;
100		if (c != '$')
101			continue;
102
103		/*
104		 * The 'before_len' variable contains the number
105		 * of characters before the '$'.
106		 */
107		before_len = i;
108		assert(i + 1 < (int)strlen(string));
109		if (string[i + 1] == '{')
110			bracketed = true;
111
112		if (string[i + 1] == '\0') {
113			log_warnx("truncated variable");
114			return (NULL);
115		}
116
117		/*
118		 * Skip '$'.
119		 */
120		i++;
121
122		if (bracketed) {
123			if (string[i + 1] == '\0') {
124				log_warnx("truncated variable");
125				return (NULL);
126			}
127
128			/*
129			 * Skip '{'.
130			 */
131			i++;
132		}
133
134		/*
135		 * The 'name_off' variable contains the number
136		 * of characters before the variable name,
137		 * including the "$" or "${".
138		 */
139		name_off = i;
140
141		for (; string[i] != '\0'; i++) {
142			c = string[i];
143			/*
144			 * XXX: Decide on the set of characters that can be
145			 *	used in a variable name.
146			 */
147			if (isalnum(c) || c == '_')
148				continue;
149
150			/*
151			 * End of variable name.
152			 */
153			if (bracketed) {
154				if (c != '}')
155					continue;
156
157				/*
158				 * The 'after_off' variable contains the number
159				 * of characters before the rest of the string,
160				 * i.e. after the variable name.
161				 */
162				after_off = i + 1;
163				assert(i > 1);
164				assert(i - 1 > name_off);
165				name_len = i - name_off;
166				break;
167			}
168
169			after_off = i;
170			assert(i > 1);
171			assert(i > name_off);
172			name_len = i - name_off;
173			break;
174		}
175
176		name = strndup(string + name_off, name_len);
177		if (name == NULL)
178			log_err(1, "strndup");
179		value = defined_find(name);
180		if (value == NULL) {
181			log_warnx("undefined variable ${%s}", name);
182			return (NULL);
183		}
184
185		/*
186		 * Concatenate it back.
187		 */
188		ret = asprintf(&expanded, "%.*s%s%s",
189		    before_len, string, value, string + after_off);
190		if (ret < 0)
191			log_err(1, "asprintf");
192
193		//log_debugx("\"%s\" expanded to \"%s\"", string, expanded);
194		free(name);
195
196		/*
197		 * Figure out where to start searching for next variable.
198		 */
199		string = expanded;
200		i = before_len + strlen(value);
201		backslashed = bracketed = false;
202		before_len = name_off = name_len = after_off = 0;
203		assert(i <= (int)strlen(string));
204	}
205
206	if (before_len != 0 || name_off != 0 || name_len != 0 || after_off != 0) {
207		log_warnx("truncated variable");
208		return (NULL);
209	}
210
211	return (expanded);
212}
213
214static void
215defined_add(const char *name, const char *value)
216{
217	struct defined_value *d;
218	const char *found;
219
220	found = defined_find(name);
221	if (found != NULL)
222		log_errx(1, "variable %s already defined", name);
223
224	log_debugx("defining variable %s=%s", name, value);
225
226	d = calloc(1, sizeof(*d));
227	if (d == NULL)
228		log_err(1, "calloc");
229	d->d_name = checked_strdup(name);
230	d->d_value = checked_strdup(value);
231
232	TAILQ_INSERT_TAIL(&defined_values, d, d_next);
233}
234
235void
236defined_parse_and_add(char *def)
237{
238	char *name, *value;
239
240	value = def;
241	name = strsep(&value, "=");
242
243	if (value == NULL || value[0] == '\0')
244		log_errx(1, "missing variable value");
245	if (name == NULL || name[0] == '\0')
246		log_errx(1, "missing variable name");
247
248	defined_add(name, value);
249}
250
251void
252defined_init(void)
253{
254	struct utsname name;
255	int error;
256
257	TAILQ_INIT(&defined_values);
258
259	error = uname(&name);
260	if (error != 0)
261		log_err(1, "uname");
262
263	defined_add("ARCH", name.machine);
264	defined_add("CPU", name.machine);
265	defined_add("DOLLAR", "$");
266	defined_add("HOST", name.nodename);
267	defined_add("OSNAME", name.sysname);
268	defined_add("OSREL", name.release);
269	defined_add("OSVERS", name.version);
270}
271