Commit Graph

47 Commits

Author SHA1 Message Date
Thomas Egerer 99bef7b686 settings: Use strtoul(3) for settings to int conversion
strtol(3) accepts values in the range of [LONG_MIN;LONG_MAX].  Based
on the architecture (32 or 64 bits), these values expand to either
0x8000000000000000/0x7fffffffffffffff for 64-bit builds, or
0x80000000/0x7fffffff for 32-bit builds.

The behavior when retrieving non-default values for charon.spi_min or
charon.spi_max, for example, depends on the architecture of the target
platform.  While 0xC000001/0xCFFFFFFE work fine on a 64-bit build, on a
32-bit build, due to the use of strtol(3), an ERANGE causes get_int()
to return the default values.

By using strtoul(3) the default is only returned if the input value
exceeds 32 or 64 bits, based on the platform.  Negative values are still
parsed correctly.

Signed-off-by: Thomas Egerer <thomas.egerer@secunet.com>
2020-03-26 15:26:59 +01:00
Tobias Brunner 97c4551ec8 Avoid inclusion of unistd.h in generated lexers
Because the file is not available on all platforms the inclusion comes
after the user options in order to disable including it.  But that means
the inclusion also follows after the defined scanner states, which are
generated as simple #defines to numbers.  If the included unistd.h e.g.
uses variables in function definitions with the same names this could
result in compilation errors.

Interactive mode has to be disabled too as it relies on isatty() from
unistd.h.  Since we don't use the scanners interactively, this is not a
problem and might even make the scanners a bit faster.

Fixes #2806.
2018-11-22 11:17:35 +01:00
Tobias Brunner 784d96e031 Fixed some typos, courtesy of codespell 2018-09-17 18:51:44 +02:00
Tobias Brunner 432e2a72b2 settings: Clarify that a key/value-pair can only occur once 2018-09-17 18:51:43 +02:00
Tobias Brunner 71dca60c31 settings: Don't allow dots in section/key names anymore
This requires config changes if filelog is used with a path that
contains dots. This path must now be defined in the `path` setting of an
arbitrarily named subsection of `filelog`.  Without that change the
whole strongswan.conf file will fail to load, which some users might
not notice immediately.
2018-09-11 18:30:18 +02:00
Tobias Brunner daa0a0cc1b settings: Fix compilation with newer versions of Clang
Depending on the actual va_list definition it's not valid to compare it
directly or assign NULL.
2018-06-29 11:47:26 +02:00
Tobias Brunner 089d5f9765 settings: Properly lock when extending sections or adding fallbacks
There was a potential chance for a race condition if the ensured section
was purged for some reason before using it later.

This also changes the behavior for NULL/empty strings via load_string*
with merge == FALSE, which now purges the config/section.
2018-06-27 14:19:35 +02:00
Tobias Brunner 35ca6a7b2f settings: Add reference feature
Similar to the `also` keyword in ipsec.conf, the new syntax allows adding
one or more references to other sections, which means all the settings and
subsections defined there are inherited (values may be overridden, even
with an empty value to clear it).

It's important to note that all subsections are inherited, so if this is
used to reference a connection in swanctl.conf all auth rounds and
children are inherited.  There is currently no syntax to limit the
inclusion level or clear inherited sections (but as mentioned, settings
in those inherited sections may be overridden).

Another property is that inherited settings or sections always follow
explicitly defined entries in the current section when they are enumerated.
This is relevant if the order is important (e.g. for auth rounds if `round`
is not specified).

References are evaluated dynamically at runtime, so referring to
sections later in the config file or included via other files is no
problem.

The colon used as separator to reference other sections may be used in
section names by writing :: (e.g. for Windows log file paths).

This is based on a patch originally written in 2016.
2018-06-27 14:19:35 +02:00
Tobias Brunner 1b67166921 Unify format of HSR copyright statements 2018-05-23 16:32:53 +02:00
Tobias Brunner 740133b12d settings: Parse assigned values in a different context
This allows us to accept characters like = or { without having to use
quoted strings.  And we can also properly warn about unexpected quoted
strings.
2018-05-23 12:01:50 +02:00
Tobias Brunner f8c20fb1c2 settings: Support CRLF in settings parser 2018-05-23 12:01:45 +02:00
Tobias Brunner b93ebb4ea9 utils: Add helper function to parse time spans from strings 2017-11-02 10:04:03 +01:00
Tobias Brunner 7cecc98e8a settings: Fix possible undefined behavior with va_start() and bool
This fixes compilation with -Werror when using Clang 4.0 (but not 3.9)
and possibly prevents undefined behavior.

According to the C standard the following applies to the second
parameter of the va_start() macro (subclause 7.16.1.4, paragraph 4):

  The parameter parmN is the identifier of the rightmost parameter
  in the variable parameter list in the function definition (the
  one just before the ...). If the parameter parmN is declared with
  the register storage class, with a function or array type, or with
  a type that is not compatible with the type that results after
  application of the default argument promotions, the behavior is
  undefined.

Because bool is usually just 1 byte and therefore smaller than int (i.e.
the result of default argument promotion) its use as last argument before
... might result in undefined behavior.  This theoretically can also
apply to enums as a compiler may use a smaller base type than int.

Since Clang 3.9 (currently in use on Travis by default) a warning is
issued about this, however, that version did not yet compare the actual
size of the argument's type, causing warnings where they are not
warranted (basically for all cases where enum types are used for the
last argument).  This was apparently fixed with Clang 4.0, which only
warns about this use of bool with va_start(), which makes sense.
2017-09-18 12:07:26 +02:00
Tobias Brunner 525cc46cab Change interface for enumerator_create_filter() callback
This avoids the unportable 5 pointer hack, but requires enumerating in
the callback.
2017-05-26 13:56:44 +02:00
Tobias Brunner 95a63bf281 Migrate all enumerators to venumerate() interface change 2017-05-26 13:56:44 +02:00
Tobias Brunner 4b5cbb3db1 settings: Add support for hex integers (0x prefix) via get_int() 2017-03-02 08:50:54 +01:00
Tobias Brunner d6c6fb1332 settings: Fix purge if order differs from alphabetical order 2017-02-07 11:00:31 +01:00
Andreas Steffen b12c53ce77 Use standard unsigned integer types 2016-03-24 18:52:48 +01:00
Tobias Brunner 8623ae9fc6 settings: Add settings_value_as_uint64() helper function 2015-11-11 15:39:49 +01:00
Tobias Brunner 08b45fc516 settings: Add methods and a constructor to parse settings from strings 2015-07-28 13:27:32 +02:00
Tobias Brunner 0dba2690c4 settings: Extend parser so we can parse settings from a string 2015-07-28 13:27:32 +02:00
Tobias Brunner 764960e8b9 settings: Add support for multi-line strings
Unterminated strings are now an error.
2015-07-28 13:27:32 +02:00
Tobias Brunner 9577b2d148 settings: Don't replace rarely used special characters 2015-07-28 13:27:31 +02:00
Tobias Brunner d148c7ab6c settings: Merge quoted strings that span multiple lines 2015-03-20 18:37:22 +01:00
Martin Willi d8ad0fc351 settings: Make loading a NULL or empty pattern a (nop-)success 2014-09-22 13:40:39 +02:00
Martin Willi 68da3bad57 settings: Use strongswan.conf used during library initialization for reload
Since 4b670a20 we require an explicit strongswan.conf to re-load configurations.
However, the define was missing in the build, breaking SIGHUP based config
reloading.

Fixes #651.
2014-09-22 13:40:39 +02:00
Martin Willi a51c48eeaa settings: Allow spaces in time settings before the optional unit 2014-07-07 16:58:05 +02:00
Martin Willi 0058e26cb0 settings: Be more strict in converting settings to specific data types
As the behavior was inconsistent for empty strings or strings with characters
appended to a number, testing the code failed on some platforms. The new rules
are more strict, returning the default if additional characters or an empty
string was found for a setting.
2014-07-07 16:57:38 +02:00
Tobias Brunner b3dd0168f1 settings: Properly match } and # in include statements
Found due to %option nodefault.  A match for } was actually missing
and # was not properly matched if it was part of an include statement
on the last line of a file that did not end with a newline.
2014-05-15 12:03:07 +02:00
Tobias Brunner c92d44f2cf settings: Eliminate performance warning
This was useful during development, but we accept that matching \n together
with %option yylineno impacts performance.
2014-05-15 12:03:07 +02:00
Tobias Brunner 66248396c6 parser-helper: Make parser_helper_file_t private 2014-05-15 11:28:10 +02:00
Tobias Brunner 4b670a20a9 settings: strongswan.conf must be loaded explicitly 2014-05-15 11:28:10 +02:00
Tobias Brunner da45f9e994 settings: Replace deprecated YYLEX_PARAM with %lex-param
With Bison 3.x support for YYLEX_PARAM has been removed and %lex-param
should be used.  Unfortunately, that option does not take expressions.
Instead we use a wrapper function that calls the lexer with the proper
scanner object, which should also be backward compatible to older Bison
versions.
2014-05-15 11:28:09 +02:00
Tobias Brunner 813e510d69 settings: Include generated header after others
Newer Bison versions declare the parser function in the header, which
requires custom types.
2014-05-15 11:28:09 +02:00
Tobias Brunner f65ac98c64 settings: Reduce log verbosity if files can't be opened
Basically reintroducing 2a38b4556e.
2014-05-15 11:28:09 +02:00
Tobias Brunner 8b43c9ba34 settings: Adopt the new order of sections and settings when replacing configs 2014-05-15 11:28:09 +02:00
Tobias Brunner 5ac20cbb87 settings: Only purge sections if necessary
Instead of removing and caching all values of a previous config, we only
do this for actually removed sections/settings.
2014-05-15 11:28:08 +02:00
Tobias Brunner f5dd274ab8 settings: Maintain order of sections and settings while enumerating 2014-05-15 11:28:08 +02:00
Tobias Brunner 2fbbea55c5 settings: Don't overwrite values in-place
This is not thread safe.  If threads are reading from pointers to existing
values they could get a partially updated invalid value.

Refactored assignment to a separate function.
2014-05-15 11:28:08 +02:00
Tobias Brunner 725c479f8b settings: Add functions to add sections and key/value pairs to a section 2014-05-15 11:28:07 +02:00
Tobias Brunner 3855dc01ec settings: Don't enumerate key/value pairs with NULL value 2014-05-15 11:28:07 +02:00
Tobias Brunner 47a3ed979b settings: Use generated parser instead of our own 2014-05-15 11:28:07 +02:00
Tobias Brunner 073d72cf49 settings: Optionally keep track of removed/replaced values 2014-05-15 11:28:06 +02:00
Tobias Brunner 1f669078ac settings: Add flex/bison based parser for strongswan.conf
This parser features several improvements over the existing one.
For instance, quoted strings (with escape sequences), unlimited includes,
relaxed newline handling (e.g. at the end of files or before/after { and }),
and the difference between empty and unset values (key = vs. key = "").

It also complains a lot more about invalid syntax. The current one accepts
pretty odd stuff (like settings or sections without name) without any
errors or warnings.
2014-05-15 11:28:06 +02:00
Tobias Brunner f99d1f7ba5 settings: Extract section and key/value pair types and helper functions
This allows us to use them in the upcoming parser.
2014-05-15 11:28:06 +02:00
Tobias Brunner 3784633fa5 settings: Use glob enumerator to load included files 2014-05-15 11:28:06 +02:00
Tobias Brunner b9b1114ab1 settings: Move to a separate folder 2014-05-15 11:28:05 +02:00