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