summaryrefslogtreecommitdiffstats
path: root/mdk-stage1/insmod-modutils/util/meta_expand.c
blob: 41fb4024c9e3466a47a58f1477d25f69ecdd3f6d (plain)
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
/*
 * Handle expansion of meta charaters
 *
 * Copyright 1999 Björn Ekwall <bj0rn@blox.se>
 *
 * "kernelversion" idea from the Debian release via:
 *	Wichert Akkerman <wakkerma@cs.leidenuniv.nl>
 *
 * Use wordexp(): idea from Tim Waugh <tim@cyberelk.demon.co.uk>
 *
 * Alpha typecast: Michal Jaegermann <michal@ellpspace.math.ualberta.ca>
 *
 * This file is part of the Linux modutils.
 *
 * This program 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#ifdef HAVE_WORDEXP
#undef HAVE_WORDEXP
#define HAVE_WORDEXP 0
#endif

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <ctype.h>
#if HAVE_WORDEXP
#include <wordexp.h>
#elif HAVE_GLOB
#include <glob.h>
#endif
#include "util.h"

/*
 * Split into words delimited by whitespace,
 * handle remaining quotes though...
 * If strip_quotes != 0 then strip one level of quotes from the line.
 */
static void split_line(GLOB_LIST *g, char *line, int strip_quotes)
{
	int len;
	char *d;
	char *e;
	char *p;
	char tmpline[PATH_MAX];

	for (p = line; *p; p = e) {
		/* Skip leading whitespace */
		while (*p && isspace(*p))
			++p;

		/* find end of word */
		d = tmpline;
		for (e = p; *e && !(isspace(*e)); ++e) {
			char match;

			/* Quote handling */
			switch (*e) {
			case '\\':
				if (!strip_quotes)
					*d++ = *e;
				break;

			case '"':
			case '\'':
				match = *e;
				if (!strip_quotes)
					*d++ = *e;
				for (++e; *e && *e != match; ++e) {
					*d++ = *e;
					if (*e == '\\' && *(e + 1) == match)
						*d++ = *++e;
				}
				if (!strip_quotes)
					*d++ = *e;
				break;

			default:
				*d++ = *e;
				break;
			}
		}

		if ((len = (int)(d - tmpline)) > 0) {
			char *str = xmalloc(len + 1);
			strncpy(str, tmpline, len);
			str[len] = '\0';
			g->pathv = (char **)xrealloc(g->pathv,
				   (g->pathc + 2) * sizeof(char *));
			g->pathv[g->pathc++] = str;
		}
	}

	if (g->pathc)
		g->pathv[g->pathc] = NULL;
}

static int glob_it(char *pt, GLOB_LIST *g)
{
#if HAVE_WORDEXP
	wordexp_t w;

	memset(&w, 0, sizeof(w));
	if (wordexp(pt, &w, WRDE_UNDEF)) {
		/*
		error("wordexp %s failed", pt);
		*/
		return -1;
	}
	/* else */
	g->pathc = w.we_wordc;
	g->pathv = w.we_wordv;

	return 0;
#elif HAVE_GLOB /* but not wordexp */
	glob_t w;

	memset(&w, 0, sizeof(w));
	if (glob(pt, GLOB_NOSORT, NULL, &w)) {
		/*
		error("glob %s failed", pt);
		*/
		return -1;
	}
	/* else */
	if (w.gl_pathc && strpbrk(w.gl_pathv[0], SHELL_META)) {
		globfree(&w);
		return -1;
	}
	g->pathc = w.gl_pathc;
	g->pathv = w.gl_pathv;

	return 0;
#else /* Neither wordexp nor glob */
	return -1;
#endif
}

/*
 * Expand the string (including meta-character) to a list of matches
 *
 * Return 0 if OK else -1
 */
int meta_expand(char *pt, GLOB_LIST *g, char *base_dir, char *version, int type)
{
	FILE *fin;
	int len = 0;
	char *line = NULL;
	char *p, *p1;
	char tmpline[PATH_MAX + 1];
	char wrk[sizeof(tmpline)];
	char tmpcmd[2*sizeof(tmpline)+20];	/* room for /bin/echo "text" */

	g->pathc = 0;
	g->pathv = NULL;

	/*
	 * Take care of version dependent expansions
	 * Needed for forced version handling
	 */
	if ((p = strchr(pt, '`')) != NULL && (type & ME_BUILTIN_COMMAND)) {
		do {
			char *s;

			for (s = p + 1; isspace(*s); ++s)
				;

			if (strncmp(s, "uname -r", 8) == 0) {
				while (*s && (*s != '`'))
					++s;
				if (*s == '`') {
					*p = '\0';
					snprintf(wrk, sizeof(wrk), "%s%s%s",
						     pt,
						     version,
						     s + 1);
					*p = '`';
				}
				strcpy(tmpline, wrk);	/* safe, same size */
				pt = tmpline;
			} else if (strncmp(s, "kernelversion", 13) == 0) {
				while (*s && (*s != '`'))
					++s;
				if (*s == '`') {
					int n;
					char *k;

					*p = '\0';
					for (n = 0, k = version; *k; ++k) {
						if (*k == '.' && ++n == 2)
							break;
					}
					snprintf(wrk, sizeof(wrk), "%s%.*s%s",
						     pt,
						     /* typecast for Alpha */
						     (int)(k - version),
						     version,
						     s + 1);
					*p = '`';
					strcpy(tmpline, wrk);	/* safe, same size */
					pt = tmpline;
				}
			} else
				break;
		} while ((p = strchr(pt, '`')) != NULL);
	}

	/*
	 * Any remaining meta-chars?
	 */
	if (strpbrk(pt, SHELL_META) == NULL) {
		/*
		 * No meta-chars.
		 * Split into words, delimited by whitespace.
		 */
		snprintf(wrk, sizeof(wrk), "%s%s", (base_dir ? base_dir : ""), pt);
		strcpy(tmpline, wrk);	/* safe, same size */
		if ((p = strtok(tmpline, " \t\n")) != NULL) {
			while (p) {
				g->pathv = (char **)xrealloc(g->pathv,
					   (g->pathc + 2) * sizeof(char *));
				g->pathv[g->pathc++] = xstrdup(p);
				p = strtok(NULL, " \t\n");
			}
		}
		if (g->pathc)
			g->pathv[g->pathc] = NULL;
		return 0;
	}
	/* else */
	/*
	 * Handle remaining meta-chars
	 */

	/*
	 * Just plain quotes?
	 */
	if (strpbrk(pt, "&();|<>$`!{}[]~=+:?*") == NULL &&
	    (p = strpbrk(pt, "\"'\\"))) {
		split_line(g, pt, 1);
		return 0;
	}

	if (strpbrk(pt, "&();|<>$`\"'\\!{}~+:[]~?*") == NULL) {
		/* Only "=" remaining, should be module options */
		split_line(g, pt, 0);
		return 0;
	}

	/*
	 * If there are meta-characters and
	 * if they are only shell glob meta-characters: do globbing
	 */
#if HAVE_WORDEXP
	if (strpbrk(pt, "&();|<>`\"'\\!{}~=+:") == NULL &&
	    strpbrk(pt, "$[]~?*"))
#else
	if (strpbrk(pt, "&();|<>$`\"'\\!{}~=+:") == NULL &&
	    strpbrk(pt, "[]~?*"))
#endif
		if ((type & ME_GLOB) && glob_it(pt, g) == 0)
			return 0;

	if (strpbrk(pt, "&();|<>$`\"'\\!{}~+:[]~?*") == NULL) {
		/* Only "=" remaining, should be module options */
		split_line(g, pt, 0);
		return 0;
	}

	/*
	 * Last resort: Use "echo".
	 * DANGER: Applying shell expansion to user supplied input is a
	 *         major security risk.  Modutils code should only do meta
	 *         expansion via shell commands for trusted data.  Basically
	 *         this means only for data in the config file.   Even that
	 *         assumes that the user cannot run modprobe as root with
	 *         their own config file.  Programs (including the kernel)
	 *         that invoke modprobe as root with user supplied input must
	 *         pass exactly one user supplied parameter and must set
	 *         safe mode.
	 */
	if (!(type & ME_SHELL_COMMAND))
		return 0;
	snprintf(wrk, sizeof(wrk), "%s%s", (base_dir ? base_dir : ""), pt);
	strcpy(tmpline, wrk);	/* safe, same size */
	snprintf(tmpcmd, sizeof(tmpcmd), "/bin/echo \"");
	for (p = tmpline, p1 = tmpcmd + strlen(tmpcmd); *p; ++p, ++p1) {
		if (*p == '"' || *p == '\\')
			*p1++ = '\\';
		*p1 = *p;
	}
	*p1++ = '"';
	*p1++ = '\0';
	if (p1 - tmpcmd > sizeof(tmpcmd)) {
		error("tmpcmd overflow, should never happen");
		exit(1);
	}
	if ((fin = popen(tmpcmd, "r")) == NULL) {
		error("Can't execute: %s", tmpcmd);
		return -1;
	}
	/* else */

	/*
	 * Collect the result
	 */
	while (fgets(tmpcmd, PATH_MAX, fin) != NULL) {
		int l = strlen(tmpcmd);

		line = (char *)xrealloc(line, len + l + 1);
		line[len] = '\0';
		strcat(line + len, tmpcmd);	/* safe, realloc */
		len += l;
	}
	pclose(fin);

	if (line) {
		/* shell used to strip one set of quotes.  Paranoia code in
		 * 2.3.20 stops that strip so we do it ourselves.
		 */
		split_line(g, line, 1);
		free(line);
	}

	return 0;
}