| | varnish-cache/lib/libvcc/vcc_source.c |
0 |
|
/*- |
1 |
|
* Copyright (c) 2006 Verdens Gang AS |
2 |
|
* Copyright (c) 2006-2015 Varnish Software AS |
3 |
|
* All rights reserved. |
4 |
|
* |
5 |
|
* Author: Poul-Henning Kamp <phk@phk.freebsd.dk> |
6 |
|
* |
7 |
|
* SPDX-License-Identifier: BSD-2-Clause |
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 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 |
|
#include "config.h" |
32 |
|
|
33 |
|
#include <glob.h> |
34 |
|
#include <stdlib.h> |
35 |
|
#include <string.h> |
36 |
|
#include <unistd.h> |
37 |
|
|
38 |
|
#include "vcc_compile.h" |
39 |
|
|
40 |
|
#include "vfil.h" |
41 |
|
|
42 |
|
struct source * |
43 |
179960 |
vcc_new_source(const char *src, const char *kind, const char *name) |
44 |
|
{ |
45 |
|
struct source *sp; |
46 |
|
|
47 |
179960 |
AN(src); |
48 |
179960 |
AN(name); |
49 |
179960 |
ALLOC_OBJ(sp, SOURCE_MAGIC); |
50 |
179960 |
AN(sp); |
51 |
179960 |
REPLACE(sp->name, name); |
52 |
179960 |
sp->kind = kind; |
53 |
179960 |
sp->b = src; |
54 |
179960 |
sp->e = strchr(src, '\0'); |
55 |
179960 |
VTAILQ_INIT(&sp->src_tokens); |
56 |
179960 |
return (sp); |
57 |
|
} |
58 |
|
|
59 |
|
/*--------------------------------------------------------------------*/ |
60 |
|
|
61 |
|
struct source * |
62 |
2080 |
vcc_file_source(struct vcc *tl, const char *fn) |
63 |
|
{ |
64 |
|
char *f, *fnp; |
65 |
|
struct source *sp; |
66 |
|
|
67 |
2080 |
if (!tl->unsafe_path && strchr(fn, '/') != NULL) { |
68 |
40 |
VSB_printf(tl->sb, "VCL filename '%s' is unsafe.\n", fn); |
69 |
40 |
tl->err = 1; |
70 |
40 |
return (NULL); |
71 |
|
} |
72 |
2040 |
f = NULL; |
73 |
2040 |
if (VFIL_searchpath(tl->vcl_path, NULL, &f, fn, &fnp) || f == NULL) { |
74 |
80 |
VSB_printf(tl->sb, "Cannot read file '%s' (%s)\n", |
75 |
40 |
fnp != NULL ? fnp : fn, strerror(errno)); |
76 |
40 |
free(fnp); |
77 |
40 |
tl->err = 1; |
78 |
40 |
return (NULL); |
79 |
|
} |
80 |
2000 |
sp = vcc_new_source(f, "file", fnp); |
81 |
2000 |
free(fnp); |
82 |
2000 |
return (sp); |
83 |
2080 |
} |
84 |
|
|
85 |
|
/*--------------------------------------------------------------------*/ |
86 |
|
|
87 |
|
static void |
88 |
1600 |
vcc_include_file(struct vcc *tl, const struct source *src_sp, |
89 |
|
const char *filename, const struct token *parent_token) |
90 |
|
{ |
91 |
|
struct source *sp; |
92 |
|
|
93 |
1600 |
sp = vcc_file_source(tl, filename); |
94 |
1600 |
if (sp == NULL) |
95 |
40 |
return; |
96 |
|
|
97 |
1560 |
sp->parent = src_sp; |
98 |
1560 |
sp->parent_tok = parent_token; |
99 |
1560 |
vcc_lex_source(tl, sp, 0); |
100 |
1600 |
} |
101 |
|
|
102 |
|
/*--------------------------------------------------------------------*/ |
103 |
|
|
104 |
|
static void |
105 |
160 |
vcc_include_glob_file(struct vcc *tl, const struct source *src_sp, |
106 |
|
const char *filename, const struct token *parent_token) |
107 |
|
{ |
108 |
|
glob_t g[1]; |
109 |
|
unsigned u; |
110 |
|
int i; |
111 |
|
|
112 |
160 |
memset(g, 0, sizeof g); |
113 |
160 |
i = glob(filename, 0, NULL, g); |
114 |
160 |
switch (i) { |
115 |
|
case 0: |
116 |
320 |
for (u = 0; !tl->err && u < g->gl_pathc; u++) { |
117 |
200 |
vcc_include_file( |
118 |
200 |
tl, src_sp, g->gl_pathv[u], parent_token); |
119 |
200 |
} |
120 |
120 |
break; |
121 |
|
case GLOB_NOMATCH: |
122 |
40 |
VSB_cat(tl->sb, "glob pattern matched no files.\n"); |
123 |
40 |
tl->err = 1; |
124 |
40 |
break; |
125 |
|
default: |
126 |
0 |
VSB_printf(tl->sb, "glob(3) expansion failed (%d)\n", i); |
127 |
0 |
tl->err = 1; |
128 |
0 |
break; |
129 |
|
} |
130 |
160 |
globfree(g); |
131 |
160 |
} |
132 |
|
|
133 |
|
/*-------------------------------------------------------------------- |
134 |
|
* NB: We cannot use vcc_ErrWhere2() on tokens which are not on the |
135 |
|
* NB: tl->tokens list. |
136 |
|
*/ |
137 |
|
|
138 |
|
static struct token * |
139 |
1760 |
vcc_lex_include(struct vcc *tl, const struct source *src_sp, struct token *t) |
140 |
|
{ |
141 |
|
struct token *tok1; |
142 |
1760 |
int i, glob_flag = 0; |
143 |
1760 |
struct vsb *vsb = NULL; |
144 |
|
const char *filename; |
145 |
|
const char *p; |
146 |
|
|
147 |
1760 |
assert(vcc_IdIs(t, "include")); |
148 |
|
|
149 |
1760 |
tok1 = VTAILQ_NEXT(t, src_list); |
150 |
1760 |
AN(tok1); |
151 |
|
|
152 |
1960 |
while (1) { |
153 |
1960 |
t = VTAILQ_NEXT(tok1, src_list); |
154 |
1960 |
AN(t); |
155 |
1960 |
i = vcc_IsFlagRaw(tl, tok1, t); |
156 |
1960 |
if (i < 0) |
157 |
1760 |
break; |
158 |
200 |
if (vcc_IdIs(t, "glob")) { |
159 |
200 |
glob_flag = i; |
160 |
200 |
} else { |
161 |
0 |
VSB_cat(tl->sb, "Unknown include flag:\n"); |
162 |
0 |
vcc_ErrWhere(tl, t); |
163 |
0 |
return (t); |
164 |
|
} |
165 |
200 |
tok1 = VTAILQ_NEXT(t, src_list); |
166 |
200 |
AN(tok1); |
167 |
|
} |
168 |
|
|
169 |
1760 |
if (tok1->tok != CSTR) { |
170 |
80 |
VSB_cat(tl->sb, |
171 |
|
"include not followed by string constant.\n"); |
172 |
80 |
vcc_ErrWhere(tl, tok1); |
173 |
80 |
return (t); |
174 |
|
} |
175 |
1680 |
t = VTAILQ_NEXT(tok1, src_list); |
176 |
1680 |
AN(t); |
177 |
|
|
178 |
1680 |
if (t->tok != ';') { |
179 |
40 |
VSB_cat(tl->sb, |
180 |
|
"include <string> not followed by semicolon.\n"); |
181 |
40 |
vcc_ErrWhere(tl, tok1); |
182 |
40 |
return (t); |
183 |
|
} |
184 |
|
|
185 |
1640 |
filename = tok1->dec; |
186 |
|
|
187 |
1640 |
if (filename[0] == '.' && filename[1] == '/') { |
188 |
|
/* |
189 |
|
* Nested include filenames, starting with "./" are |
190 |
|
* resolved relative to the VCL file which contains |
191 |
|
* the include directive. |
192 |
|
*/ |
193 |
400 |
if (src_sp->name[0] != '/') { |
194 |
80 |
VSB_cat(tl->sb, |
195 |
|
"include \"./xxxxx\"; needs absolute " |
196 |
|
"filename of including file.\n"); |
197 |
80 |
vcc_ErrWhere(tl, tok1); |
198 |
80 |
return (t); |
199 |
|
} |
200 |
320 |
vsb = VSB_new_auto(); |
201 |
320 |
AN(vsb); |
202 |
320 |
p = strrchr(src_sp->name, '/'); |
203 |
320 |
AN(p); |
204 |
320 |
VSB_bcat(vsb, src_sp->name, p - src_sp->name); |
205 |
320 |
VSB_cat(vsb, filename + 1); |
206 |
320 |
AZ(VSB_finish(vsb)); |
207 |
320 |
filename = VSB_data(vsb); |
208 |
320 |
} |
209 |
|
|
210 |
1560 |
if (glob_flag) |
211 |
160 |
vcc_include_glob_file(tl, src_sp, filename, tok1); |
212 |
|
else |
213 |
1400 |
vcc_include_file(tl, src_sp, filename, tok1); |
214 |
1560 |
if (vsb != NULL) |
215 |
320 |
VSB_destroy(&vsb); |
216 |
1560 |
if (tl->err) |
217 |
280 |
vcc_ErrWhere(tl, tok1); |
218 |
1560 |
return (t); |
219 |
1760 |
} |
220 |
|
|
221 |
|
void |
222 |
179960 |
vcc_lex_source(struct vcc *tl, struct source *src_sp, int eoi) |
223 |
|
{ |
224 |
|
struct token *t; |
225 |
|
const struct source *sp1; |
226 |
|
|
227 |
179960 |
CHECK_OBJ_NOTNULL(src_sp, SOURCE_MAGIC); |
228 |
|
|
229 |
182040 |
for (sp1 = src_sp->parent; sp1 != NULL; sp1 = sp1->parent) { |
230 |
2160 |
if (!strcmp(sp1->name, src_sp->name) && |
231 |
80 |
!strcmp(sp1->kind, src_sp->kind)) { |
232 |
160 |
VSB_printf(tl->sb, |
233 |
|
"Recursive use of %s \"%s\"\n\n", |
234 |
80 |
src_sp->kind, src_sp->name); |
235 |
80 |
tl->err = 1; |
236 |
80 |
return; |
237 |
|
} |
238 |
2080 |
} |
239 |
|
|
240 |
179880 |
VTAILQ_INSERT_TAIL(&tl->sources, src_sp, list); |
241 |
179880 |
src_sp->idx = tl->nsources++; |
242 |
|
|
243 |
179880 |
vcc_Lexer(tl, src_sp); |
244 |
179880 |
if (tl->err) |
245 |
920 |
return; |
246 |
95973920 |
VTAILQ_FOREACH(t, &src_sp->src_tokens, src_list) { |
247 |
95855400 |
if (!eoi && t->tok == EOI) |
248 |
59960 |
break; |
249 |
|
|
250 |
95795440 |
if (t->tok == ID && vcc_IdIs(t, "include")) { |
251 |
1760 |
t = vcc_lex_include(tl, src_sp, t); |
252 |
1760 |
} else { |
253 |
95793680 |
VTAILQ_INSERT_TAIL(&tl->tokens, t, list); |
254 |
|
} |
255 |
95795440 |
if (tl->err) |
256 |
480 |
return; |
257 |
95794960 |
} |
258 |
179960 |
} |