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 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
|
/* -*- flex-mode -*- */
%option nounput
%option noinput
%{
/* Libreswan config file parser (parser.l)
* Copyright (C) 2001 Mathieu Lafon - Arkoon Network Security
* Copyright (C) 2003-2007 Michael Richardson <mcr@xelerance.com>
* Copyright (C) 2008, 2014 D. Hugh Redelmeier <hugh@mimosa.com>
* Copyright (C) 2012 Wes Hardaker <opensource@hardakers.net>
* Copyright (C) 2013 Philippe Vouters <Philippe.Vouters@laposte.net>
* Copyright (C) 2013 Paul Wouters <pwouters@redhat.com>
*
* 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. See <https://www.gnu.org/licenses/gpl2.txt>.
*
* 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.
*
*/
/*
* The generation of this code tends to always give us an unsigned vs signed
* warning on one of our many OS + compiler + flex + arch combinations.
* I'm just fed up with them... Paul
*/
#pragma GCC diagnostic ignored "-Wsign-compare"
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
#include <limits.h>
struct logger;
#define YY_DECL int yylex(struct logger *logger)
YY_DECL;
#include "ipsecconf/keywords.h"
#define YYDEBUG 1 /* HACK! for ipsecconf/parser.h AND parser.tab.h */
#include "ipsecconf/parser.h" /* includes parser.tab.h */
#include "lswlog.h"
#include "lswglob.h"
#include "lswalloc.h"
#define MAX_INCLUDE_DEPTH 10
int lex_verbosity = 0; /* how much tracing output to show */
char rootdir[PATH_MAX]; /* when evaluating paths, prefix this to them */
char rootdir2[PATH_MAX]; /* or... try this one too */
static bool parser_y_eof(struct logger *logger);
/* we want no actual output! */
#define ECHO
struct ic_inputsource {
YY_BUFFER_STATE state;
FILE *file;
unsigned int line;
bool once;
char *filename;
int fileglobcnt;
char **fileglob;
};
static struct {
int stack_ptr;
struct ic_inputsource stack[MAX_INCLUDE_DEPTH];
} ic_private;
static struct ic_inputsource *stacktop;
const char *parser_cur_filename(void)
{
return stacktop->filename;
}
unsigned parser_cur_line(void)
{
return stacktop->line;
}
void parser_y_init (const char *name, FILE *f)
{
memset(&ic_private, 0, sizeof(ic_private));
ic_private.stack[0].line = 1;
ic_private.stack[0].once = true;
ic_private.stack[0].file = f;
ic_private.stack[0].filename = clone_str(name, "filename");
stacktop = &ic_private.stack[0];
ic_private.stack_ptr = 0;
}
static void parser_y_close(struct ic_inputsource *iis)
{
pfreeany(iis->filename);
if (iis->file != NULL) {
fclose(iis->file);
iis->file = NULL;
}
if (iis->fileglob != NULL) {
for (char **p = iis->fileglob; *p; p++) {
pfree(*p);
}
pfreeany(iis->fileglob);
}
}
static bool parser_y_nextglobfile(struct ic_inputsource *iis, struct logger *logger)
{
if (iis->fileglob == NULL) {
/* EOF */
ldbg(logger, "EOF: no .fileglob");
return false;
}
if (iis->fileglob[iis->fileglobcnt] == NULL) {
/* EOF */
ldbg(logger, "EOF: .fileglob[%u] == NULL", iis->fileglobcnt);
return false;
}
/* increment for next time */
int fcnt = iis->fileglobcnt++;
if (iis->file != NULL) {
fclose(iis->file);
iis->file = NULL;
}
pfreeany(iis->filename);
iis->line = 1;
iis->once = true;
iis->filename = clone_str(iis->fileglob[fcnt], "fileglob");
/* open the file */
FILE *f = fopen(iis->filename, "r");
if (f == NULL) {
int e = errno;
parser_warning(logger, e,
"cannot open include filename: '%s'",
iis->fileglob[fcnt]);
return false;
}
iis->file = f;
yy_switch_to_buffer(yy_create_buffer(f, YY_BUF_SIZE));
return true;
}
struct lswglob_context {
const char *filename;
const char *try;
};
static void glob_include(unsigned count, char **files,
struct lswglob_context *context,
struct logger *logger)
{
/* success */
if (ic_private.stack_ptr >= MAX_INCLUDE_DEPTH - 1) {
parser_warning(logger, /*errno*/0,
"including '%s' exceeds max inclusion depth of %u",
context->filename, MAX_INCLUDE_DEPTH);
return;
}
if (lex_verbosity > 0) {
ldbg(logger, "including file '%s' ('%s') from %s:%u",
context->filename, context->try,
stacktop->filename,
stacktop->line);
}
PASSERT(logger, ic_private.stack_ptr < sizeof(ic_private.stack) - 1);
++ic_private.stack_ptr;
stacktop = &ic_private.stack[ic_private.stack_ptr];
stacktop->state = YY_CURRENT_BUFFER;
stacktop->file = NULL;
stacktop->filename = NULL;
stacktop->fileglobcnt = 0;
stacktop->fileglob = alloc_things(char *, count + 1, "globs");
for (unsigned i = 0; i < count; i++) {
stacktop->fileglob[i] = clone_str(files[i], "glob");
}
stacktop->fileglob[count] = NULL;
parser_y_eof(logger);
}
void parser_y_include (const char *filename, struct logger *logger)
{
/*
* If there is no rootdir, but there is a rootdir2, swap them.
* This reduces the number of cases to be handled.
*/
if (rootdir[0] == '\0' && rootdir2[0] != '\0') {
strcpy(rootdir, rootdir2);
rootdir2[0] = '\0';
}
struct lswglob_context context = {
.filename = filename,
};
if (filename[0] != '/' || rootdir[0] == '\0') {
/* try plain name, with no rootdirs */
context.try = filename;
if (lswglob(context.try, "ipsec.conf", glob_include, &context, logger)) {
return;
}
/*
* Not a wildcard, throw error.
*
* XXX: throw?
*/
parser_warning(logger, /*errno*/0,
"could not open include filename: '%s'",
filename);
return;
}
/* try prefixing with rootdir */
char newname[PATH_MAX];
snprintf(newname, sizeof(newname), "%s%s", rootdir, filename);
context.try = newname;
if (lswglob(context.try, "ipsec.conf", glob_include, &context, logger)) {
return;
}
if (rootdir2[0] == '\0') {
/* not a wildcard, throw error */
parser_warning(logger, /*errno*/0,
"could not open include filename '%s' (tried '%s')",
filename, newname);
return;
}
/* try again, prefixing with rootdir2 */
char newname2[PATH_MAX];
snprintf(newname2, sizeof(newname2),
"%s%s", rootdir2, filename);
context.try = newname2;
if (lswglob(context.try, "ipsec.conf", glob_include, &context, logger)) {
return;
}
parser_warning(logger, /*errno*/0,
"could not open include filename: '%s' (tried '%s' and '%s')",
filename, newname, newname2);
return;
}
static bool parser_y_eof(struct logger *logger)
{
if (stacktop->state != YY_CURRENT_BUFFER) {
yy_delete_buffer(YY_CURRENT_BUFFER);
}
if (!parser_y_nextglobfile(stacktop, logger)) {
/* no more glob'ed files to process */
if (lex_verbosity > 0) {
int stackp = ic_private.stack_ptr;
ldbg(logger, "end of file %s", stacktop->filename);
if (stackp > 0) {
ldbg(logger, "resuming %s:%u",
ic_private.stack[stackp-1].filename,
ic_private.stack[stackp-1].line);
}
}
if (stacktop->state != YY_CURRENT_BUFFER) {
yy_switch_to_buffer(stacktop->state);
}
parser_y_close(stacktop);
if (--ic_private.stack_ptr < 0) {
return true;
}
stacktop = &ic_private.stack[ic_private.stack_ptr];
}
return false;
}
/*
* Look for one of the tokens, and set the value up right.
*
* If we don't find it, clone and return the string.
*/
static bool parse_leftright(const char *s,
const struct keyword_def *k,
const char *leftright)
{
size_t split = strlen(leftright);
if (!strncaseeq(s, leftright, strlen(leftright))) {
return false;
}
/* allow <leftright>-; s[split] could be '\0' */
if (s[split] == '-') {
split++;
}
/* keyword matches? */
if (!strcaseeq(s + split, k->keyname)) {
return false;
}
/* success */
return true;
}
/* type is really "token" type, which is actually int */
static void parser_find_keyword(const char *s, YYSTYPE *lval, struct logger *logger)
{
bool left = false;
bool right = false;
(*lval) = (YYSTYPE) {0};
const struct keyword_def *k;
for (k = ipsec_conf_keywords; k->keyname != NULL; k++) {
if (strcaseeq(s, k->keyname)) {
if ((k->validity & kv_both) == kv_both) {
left = true;
right = true;
break;
}
if (k->validity & kv_leftright) {
#if 0 /* see github#663 */
left = true;
#endif
right = true;
}
break;
}
if (k->validity & kv_leftright) {
left = parse_leftright(s, k, "left");
if (left) {
break;
}
right = parse_leftright(s, k, "right");
if (right) {
break;
}
}
}
/* if we still found nothing */
if (k->keyname == NULL) {
parser_fatal(logger, /*errno*/0, "unrecognized keyword '%s'", s);
}
/* else, set up llval.k to point, and return KEYWORD */
lval->k.keydef = k;
lval->k.keyleft = left;
lval->k.keyright = right;
}
%}
/* lexical states:
*
* INITIAL: pre-defined and default lex state
*
* COMMENT_KEY, KEY: just matched the "x-comment", other keyword;
* expecting '='
*
* VALUE: just matched '=' in KEY state; matches a quoted/braced/raw
* string; returns to INITIAL state
*
* COMMENT_VALUE: just matched '=' in COMMENT_KEY state; matches
* everything up to \n as a string; returns to INITIAL state
*/
%x KEY VALUE COMMENT_KEY COMMENT_VALUE
%%
<<EOF>> {
ldbg(logger, "EOF: stacktop->filename = %s",
stacktop->filename == NULL ? "<null>" : stacktop->filename);
/*
* Add a newline at the end of the file in case one was missing.
* This code assumes that EOF is sticky:
* that it can be detected repeatedly.
*/
if (stacktop->once) {
stacktop->once = false;
return EOL;
}
/*
* we've finished this file:
* continue with the file it was included from (if any)
*/
if (parser_y_eof(logger)) {
yyterminate();
}
}
^[\t ]*#.*\n {
/* eat comment lines */
stacktop->line++;
}
^[\t ]*\n {
/* eat blank lines */
stacktop->line++;
}
^[\t ]+ return FIRST_SPACES;
<INITIAL>[\t ]+ /* ignore spaces in line */ ;
<VALUE>%forever {
/* a number, really 0 */
yylval.s = clone_str("0", "string");
BEGIN INITIAL;
return STRING;
}
<KEY>[\t ] /* eat blanks */
<KEY>\n {
/* missing equals? */
stacktop->line++;
BEGIN INITIAL;
return EOL;
}
<KEY>= { BEGIN VALUE; return EQUAL; }
<VALUE>[\t ] /* eat blanks (not COMMENT_VALUE) */
<VALUE>\n {
/* missing value? (not COMMENT_VALUE) */
stacktop->line++;
BEGIN INITIAL;
return EOL;
}
<VALUE>\"[^\"\n]*\" {
/* "string" */
char *s = yytext + 1;
int len = strlen(s);
assert(len>0);
/* remove trailing " */
s[len-1] = '\0';
yylval.s = clone_str(s, "yyval.s");
BEGIN INITIAL;
return STRING;
}
<VALUE>\{[^\"\n]*\} {
/* { string-without-quotes } */
char *s = yytext + 1;
int len = strlen(s);
assert(len > 0);
/* remove trailing } */
s[len-1] = '\0';
yylval.s = clone_str(s, "yyval.s");
BEGIN INITIAL;
return STRING;
}
<VALUE>[^\" \t\n]+ {
/* string-without-quotes-or-blanks */
yylval.s = clone_str(yytext, "string-without-quotes-or-blanks");
BEGIN INITIAL;
return STRING;
}
<VALUE>[^\{} \t\n]+ {
/* string-without-braces-or-blanks */
yylval.s = clone_str(yytext, "string-without-braces-or-blanks");
BEGIN INITIAL;
return STRING;
}
<INITIAL>\n {
stacktop->line++;
return EOL;
}
= { BEGIN VALUE; return EQUAL; }
version { BEGIN VALUE; return VERSION; }
config return CONFIG;
setup return SETUP;
conn { BEGIN VALUE; return CONN; }
include { BEGIN VALUE; return INCLUDE; }
[Xx][_-][^\"= \t\n]+ {
yylval.s = clone_str(yytext, "X-s");
BEGIN COMMENT_KEY;
return COMMENT;
}
<COMMENT_KEY>[\t ] /* eat blanks */
<COMMENT_KEY>= {
BEGIN COMMENT_VALUE;
return EQUAL;
}
<COMMENT_KEY>\n {
/* missing equals? */
stacktop->line++;
BEGIN INITIAL;
return EOL;
}
<COMMENT_VALUE>\n {
BEGIN INITIAL;
return EOL;
}
<COMMENT_VALUE>[^\n]* {
yylval.s = clone_str(yytext, "comment-value");
BEGIN INITIAL;
return STRING;
}
[^\"= \t\n]+ {
parser_find_keyword(yytext, &yylval, logger);
BEGIN KEY;
return KEYWORD;
}
#.* { /* eat comment to end of line */ }
. {
parser_warning(logger, /*errno*/0,
"unrecognized: %s", yytext);
}
%%
int yywrap(void) {
return 1;
}
|