| | 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 |
37464 |
vcc_new_source(const char *src, const char *kind, const char *name) |
| 44 |
|
{ |
| 45 |
|
struct source *sp; |
| 46 |
|
|
| 47 |
37464 |
AN(src); |
| 48 |
37464 |
AN(name); |
| 49 |
37464 |
ALLOC_OBJ(sp, SOURCE_MAGIC); |
| 50 |
37464 |
AN(sp); |
| 51 |
37464 |
REPLACE(sp->name, name); |
| 52 |
37464 |
sp->kind = kind; |
| 53 |
37464 |
sp->b = src; |
| 54 |
37464 |
sp->e = strchr(src, '\0'); |
| 55 |
37464 |
VTAILQ_INIT(&sp->src_tokens); |
| 56 |
37464 |
return (sp); |
| 57 |
|
} |
| 58 |
|
|
| 59 |
|
/*--------------------------------------------------------------------*/ |
| 60 |
|
|
| 61 |
|
struct source * |
| 62 |
424 |
vcc_file_source(struct vcc *tl, const char *fn) |
| 63 |
|
{ |
| 64 |
|
char *f, *fnp; |
| 65 |
|
struct source *sp; |
| 66 |
|
|
| 67 |
424 |
if (!tl->unsafe_path && strchr(fn, '/') != NULL) { |
| 68 |
8 |
VSB_printf(tl->sb, "VCL filename '%s' is unsafe.\n", fn); |
| 69 |
8 |
tl->err = 1; |
| 70 |
8 |
return (NULL); |
| 71 |
|
} |
| 72 |
416 |
f = NULL; |
| 73 |
416 |
if (VFIL_searchpath(tl->vcl_path, NULL, &f, fn, &fnp) || f == NULL) { |
| 74 |
16 |
VSB_printf(tl->sb, "Cannot read file '%s' (%s)\n", |
| 75 |
8 |
fnp != NULL ? fnp : fn, strerror(errno)); |
| 76 |
8 |
free(fnp); |
| 77 |
8 |
tl->err = 1; |
| 78 |
8 |
return (NULL); |
| 79 |
|
} |
| 80 |
408 |
sp = vcc_new_source(f, "file", fnp); |
| 81 |
408 |
free(fnp); |
| 82 |
408 |
return (sp); |
| 83 |
424 |
} |
| 84 |
|
|
| 85 |
|
/*--------------------------------------------------------------------*/ |
| 86 |
|
|
| 87 |
|
static void |
| 88 |
320 |
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 |
320 |
sp = vcc_file_source(tl, filename); |
| 94 |
320 |
if (sp == NULL) |
| 95 |
8 |
return; |
| 96 |
|
|
| 97 |
312 |
sp->parent = src_sp; |
| 98 |
312 |
sp->parent_tok = parent_token; |
| 99 |
312 |
vcc_lex_source(tl, sp, 0); |
| 100 |
320 |
} |
| 101 |
|
|
| 102 |
|
/*--------------------------------------------------------------------*/ |
| 103 |
|
|
| 104 |
|
static void |
| 105 |
40 |
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 |
40 |
if (filename[0] != '/' && (filename[0] != '.' || filename[1] != '/')) { |
| 113 |
8 |
VSB_cat(tl->sb, |
| 114 |
|
"+glob can only be used with absolute paths or relative " |
| 115 |
|
"paths starting with './'\n"); |
| 116 |
8 |
tl->err = 1; |
| 117 |
8 |
return; |
| 118 |
|
} |
| 119 |
32 |
memset(g, 0, sizeof g); |
| 120 |
32 |
i = glob(filename, 0, NULL, g); |
| 121 |
32 |
switch (i) { |
| 122 |
|
case 0: |
| 123 |
64 |
for (u = 0; !tl->err && u < g->gl_pathc; u++) { |
| 124 |
40 |
vcc_include_file( |
| 125 |
40 |
tl, src_sp, g->gl_pathv[u], parent_token); |
| 126 |
40 |
} |
| 127 |
24 |
break; |
| 128 |
|
case GLOB_NOMATCH: |
| 129 |
8 |
VSB_cat(tl->sb, "glob pattern matched no files.\n"); |
| 130 |
8 |
tl->err = 1; |
| 131 |
8 |
break; |
| 132 |
|
default: |
| 133 |
0 |
VSB_printf(tl->sb, "glob(3) expansion failed (%d)\n", i); |
| 134 |
0 |
tl->err = 1; |
| 135 |
0 |
break; |
| 136 |
|
} |
| 137 |
32 |
globfree(g); |
| 138 |
40 |
} |
| 139 |
|
|
| 140 |
|
/*-------------------------------------------------------------------- |
| 141 |
|
* NB: We cannot use vcc_ErrWhere2() on tokens which are not on the |
| 142 |
|
* NB: tl->tokens list. |
| 143 |
|
*/ |
| 144 |
|
|
| 145 |
|
static struct token * |
| 146 |
360 |
vcc_lex_include(struct vcc *tl, const struct source *src_sp, struct token *t) |
| 147 |
|
{ |
| 148 |
|
struct token *tok1; |
| 149 |
360 |
int i, glob_flag = 0; |
| 150 |
360 |
struct vsb *vsb = NULL; |
| 151 |
|
const char *filename; |
| 152 |
|
const char *p; |
| 153 |
|
|
| 154 |
360 |
assert(vcc_IdIs(t, "include")); |
| 155 |
|
|
| 156 |
360 |
tok1 = VTAILQ_NEXT(t, src_list); |
| 157 |
360 |
AN(tok1); |
| 158 |
|
|
| 159 |
408 |
while (1) { |
| 160 |
408 |
t = VTAILQ_NEXT(tok1, src_list); |
| 161 |
408 |
AN(t); |
| 162 |
408 |
i = vcc_IsFlagRaw(tl, tok1, t); |
| 163 |
408 |
if (i < 0) |
| 164 |
360 |
break; |
| 165 |
48 |
if (vcc_IdIs(t, "glob")) { |
| 166 |
48 |
glob_flag = i; |
| 167 |
48 |
} else { |
| 168 |
0 |
VSB_cat(tl->sb, "Unknown include flag:\n"); |
| 169 |
0 |
vcc_ErrWhere(tl, t); |
| 170 |
0 |
return (t); |
| 171 |
|
} |
| 172 |
48 |
tok1 = VTAILQ_NEXT(t, src_list); |
| 173 |
48 |
AN(tok1); |
| 174 |
|
} |
| 175 |
|
|
| 176 |
360 |
if (tok1->tok != CSTR) { |
| 177 |
16 |
VSB_cat(tl->sb, |
| 178 |
|
"include not followed by string constant.\n"); |
| 179 |
16 |
vcc_ErrWhere(tl, tok1); |
| 180 |
16 |
return (t); |
| 181 |
|
} |
| 182 |
344 |
t = VTAILQ_NEXT(tok1, src_list); |
| 183 |
344 |
AN(t); |
| 184 |
|
|
| 185 |
344 |
if (t->tok != ';') { |
| 186 |
8 |
VSB_cat(tl->sb, |
| 187 |
|
"include <string> not followed by semicolon.\n"); |
| 188 |
8 |
vcc_ErrWhere(tl, tok1); |
| 189 |
8 |
return (t); |
| 190 |
|
} |
| 191 |
|
|
| 192 |
336 |
filename = tok1->dec; |
| 193 |
|
|
| 194 |
336 |
if (filename[0] == '.' && filename[1] == '/') { |
| 195 |
|
/* |
| 196 |
|
* Nested include filenames, starting with "./" are |
| 197 |
|
* resolved relative to the VCL file which contains |
| 198 |
|
* the include directive. |
| 199 |
|
*/ |
| 200 |
80 |
if (src_sp->name[0] != '/') { |
| 201 |
16 |
VSB_cat(tl->sb, |
| 202 |
|
"include \"./xxxxx\"; needs absolute " |
| 203 |
|
"filename of including file.\n"); |
| 204 |
16 |
vcc_ErrWhere(tl, tok1); |
| 205 |
16 |
return (t); |
| 206 |
|
} |
| 207 |
64 |
vsb = VSB_new_auto(); |
| 208 |
64 |
AN(vsb); |
| 209 |
64 |
p = strrchr(src_sp->name, '/'); |
| 210 |
64 |
AN(p); |
| 211 |
64 |
VSB_bcat(vsb, src_sp->name, p - src_sp->name); |
| 212 |
64 |
VSB_cat(vsb, filename + 1); |
| 213 |
64 |
AZ(VSB_finish(vsb)); |
| 214 |
64 |
filename = VSB_data(vsb); |
| 215 |
64 |
} |
| 216 |
|
|
| 217 |
320 |
if (glob_flag) |
| 218 |
40 |
vcc_include_glob_file(tl, src_sp, filename, tok1); |
| 219 |
|
else |
| 220 |
280 |
vcc_include_file(tl, src_sp, filename, tok1); |
| 221 |
320 |
if (vsb != NULL) |
| 222 |
64 |
VSB_destroy(&vsb); |
| 223 |
320 |
if (tl->err) |
| 224 |
64 |
vcc_ErrWhere(tl, tok1); |
| 225 |
320 |
return (t); |
| 226 |
360 |
} |
| 227 |
|
|
| 228 |
|
void |
| 229 |
37464 |
vcc_lex_source(struct vcc *tl, struct source *src_sp, int eoi) |
| 230 |
|
{ |
| 231 |
|
struct token *t; |
| 232 |
|
const struct source *sp1; |
| 233 |
|
|
| 234 |
37464 |
CHECK_OBJ_NOTNULL(src_sp, SOURCE_MAGIC); |
| 235 |
|
|
| 236 |
37880 |
for (sp1 = src_sp->parent; sp1 != NULL; sp1 = sp1->parent) { |
| 237 |
432 |
if (!strcmp(sp1->name, src_sp->name) && |
| 238 |
16 |
!strcmp(sp1->kind, src_sp->kind)) { |
| 239 |
32 |
VSB_printf(tl->sb, |
| 240 |
|
"Recursive use of %s \"%s\"\n\n", |
| 241 |
16 |
src_sp->kind, src_sp->name); |
| 242 |
16 |
tl->err = 1; |
| 243 |
16 |
return; |
| 244 |
|
} |
| 245 |
416 |
} |
| 246 |
|
|
| 247 |
37448 |
VTAILQ_INSERT_TAIL(&tl->sources, src_sp, list); |
| 248 |
37448 |
src_sp->idx = tl->nsources++; |
| 249 |
|
|
| 250 |
37448 |
vcc_Lexer(tl, src_sp); |
| 251 |
37448 |
if (tl->err) |
| 252 |
192 |
return; |
| 253 |
22428176 |
VTAILQ_FOREACH(t, &src_sp->src_tokens, src_list) { |
| 254 |
22403496 |
if (!eoi && t->tok == EOI) |
| 255 |
12472 |
break; |
| 256 |
|
|
| 257 |
22391024 |
if (t->tok == ID && vcc_IdIs(t, "include")) { |
| 258 |
360 |
t = vcc_lex_include(tl, src_sp, t); |
| 259 |
360 |
} else { |
| 260 |
22390664 |
VTAILQ_INSERT_TAIL(&tl->tokens, t, list); |
| 261 |
|
} |
| 262 |
22391024 |
if (tl->err) |
| 263 |
104 |
return; |
| 264 |
22390920 |
} |
| 265 |
37464 |
} |