Commit Graph

51 Commits

Author SHA1 Message Date
Lennart Poettering 10d7126365 json: add new json format flag for disabling JSON output
This adds a new flag JSON_FORMAT_OFF that is a marker for "no JSON
output please!".

Of course, this flag sounds pointless in a JSON implementation, however
this is useful in code that can generate JSON output, but also more
human friendly output (for example our table formatters).

With this in place various tools that so far maintained one boolean
field "arg_json" that controlled whether JSON output was requested at
all and another field "arg_json_format_flags" for selecing the precise
json output flags may merge them into one, simplifying code a bit.
2021-01-09 17:35:58 +01:00
Lennart Poettering 2d64d2b955 json: add APIs for quickly inserting hex blobs into as JSON strings
This is similar to the base64 support, but fixed-size hash values are
typically preferably presented as series of hex values, hence store them
here like that too.
2020-12-17 20:01:17 +01:00
Yu Watanabe db9ecf0501 license: LGPL-2.1+ -> LGPL-2.1-or-later 2020-11-09 13:23:58 +09:00
Lennart Poettering b97e3e3d7a json: also add explicit dispatchers for 'int' and 'unsigned' 2020-08-26 16:47:32 +02:00
Lennart Poettering 0710343ce7 json: add support for byte arrays to json builder 2020-08-26 16:47:16 +02:00
Lennart Poettering 0b9481cf2e json: add helpers for dealing with id128 + strv 2020-08-12 22:39:43 +02:00
Lennart Poettering 94600eeb29 json: when making a copy of a json variant, propagate the sensitive bit
Let's make sure we never lose the bit when copying a variant, after all
the data contained is still going to be sensitive after the copy.
2020-04-29 16:32:46 +02:00
Lennart Poettering 7a8867abfa user-util: rework how we validate user names
This reworks the user validation infrastructure. There are now two
modes. In regular mode we are strict and test against a strict set of
valid chars. And in "relaxed" mode we just filter out some really
obvious, dangerous stuff. i.e. strict is whitelisting what is OK, but
"relaxed" is blacklisting what is really not OK.

The idea is that we use strict mode whenver we allocate a new user
(i.e. in sysusers.d or homed), while "relaxed" mode is when we process
users registered elsewhere, (i.e. userdb, logind, …)

The requirements on user name validity vary wildly. SSSD thinks its fine
to embedd "@" for example, while the suggested NAME_REGEX field on
Debian does not even allow uppercase chars…

This effectively liberaralizes a lot what we expect from usernames.

The code that warns about questionnable user names is now optional and
only used at places such as unit file parsing, so that it doesn't show
up on every userdb query, but only when processing configuration files
that know better.

Fixes: #15149 #15090
2020-04-08 17:11:20 +02:00
Lennart Poettering 2d8143048b json: add new output flag JSON_PRETTY_AUTO
This takes inspiration from JSON_COLOR_AUTO: it will automatically map
to JSON_PRETTY if connected to a TTY and JSON_NEWLINE otherwise.
2019-12-02 09:47:00 +01:00
Lennart Poettering 19a209cc71 json: add const string dispatcher
This adds json_dispatch_const_string() which is similar to
json_dispatch_string() but doesn't store a strdup()'ed copy of the
string, but a pointer directly into the JSON record.

This should simplify cases where the json variant sticks around long
enough anyway.
2019-12-02 09:47:00 +01:00
Lennart Poettering e4defdc4b0 json: teach json_build() to build arrays from C arrays of JsonVariant 2019-12-02 09:47:00 +01:00
Lennart Poettering a42ef715a2 json: add more dispatch helpers 2019-12-02 09:47:00 +01:00
Lennart Poettering a832b08e6e json: add json_variant_set_field_integer() and json_variant_set_field_boolean() helpers 2019-12-02 09:47:00 +01:00
Lennart Poettering faca141c5f json: add json_variant_unbase64() helper 2019-12-02 09:47:00 +01:00
Lennart Poettering 0b1f2e8a06 json: add new flag for forcing a flush after dumping json data to file
This is particularly useful when no trailing \n is generated, i.e. stdio
doesn't flush the output on its own.
2019-12-02 09:47:00 +01:00
Lennart Poettering 0ac0787e30 json: add explicit log call for ENOMEM 2019-12-02 09:47:00 +01:00
Lennart Poettering aafa52ab83 json: add ability to generate empty arrays/objects in json builder 2019-12-02 09:47:00 +01:00
Lennart Poettering 886b0c93a8 json: allow putting together base64 fields with json_build() 2019-12-02 09:47:00 +01:00
Lennart Poettering 21e2151107 json: add new helper json_variant_append_array() 2019-12-02 09:47:00 +01:00
Lennart Poettering cc164891da json: add new helper json_variant_new_base64() 2019-12-02 09:47:00 +01:00
Lennart Poettering b7fc90a2e6 json: add concept of normalization
Let's add a concept of normalization: as preparation for signing json
records let's add a mechanism to bring JSON records into a well-defined
order so that we can safely validate JSON records.

This adds two booleans to each JsonVariant object: "sorted" and
"normalized". The latter indicates whether a variant is fully sorted
(i.e. all keys of objects listed in alphabetical order) recursively down
the tree. The former is a weaker property: it only checks whether the
keys of the object itself are sorted. All variants which are
"normalized" are also "sorted", but not vice versa.

The knowledge of the "sorted" property is then used to optimize
searching for keys in the variant by using bisection.

Both properties are determined at the moment the variants are allocated.
Since our objects are immutable this is safe.
2019-12-02 09:47:00 +01:00
Lennart Poettering ca409a59c8 json: add json_variant_merge() helper 2019-12-02 09:47:00 +01:00
Lennart Poettering 15f1fb3e3e json: add json_variant_set_field_string() and json_variant_set_field_unsigned() 2019-12-02 09:47:00 +01:00
Lennart Poettering 22f14d6b02 json: add json_variant_strv() helper that converts a json variant to an strv
Only works for arrays of strings, of course.
2019-12-02 09:47:00 +01:00
Lennart Poettering ba23dbf1eb json: optionally, make string checks stricter when dispatching strings 2019-12-02 09:47:00 +01:00
Lennart Poettering d642f640bf json: add flags parameter to json_parse_file(), for parsing "sensitive" data
This will call json_variant_sensitive() internally while parsing for
each allocated sub-variant. This is better than calling it a posteriori
at the end, because partially parsed variants will always be properly
erased from memory this way.
2019-12-02 09:47:00 +01:00
Lennart Poettering f325aaf341 json: add json_parse_file_at() helper
This is an "at" function, similar to json_parse_file().
2019-12-02 09:47:00 +01:00
Lennart Poettering 83bc6cb792 json: add a new "sensitive" flags for JsonVariant objects
An object marked with this flag will be erased from memory when it is
freed. This is useful for dealing with sensitive data (key material,
passphrases) encoded in JSON objects.
2019-12-02 09:47:00 +01:00
Lennart Poettering 78a41236e4 json: add new json_variant_set_field() helper 2019-12-02 09:47:00 +01:00
Lennart Poettering f2ff34ff2a json: add new API json_variant_filter() for dropping fields from objects 2019-12-02 09:47:00 +01:00
Lennart Poettering e787b211a5 json: add new json_variant_is_blank_{object,array}() helpers 2019-12-02 09:47:00 +01:00
Zbigniew Jędrzejewski-Szmek ab1a1ba5dc Drop trailing slash from assert_cc() definition
We use assert_cc(...); almost everywhere. Let's always require that.

https://github.com/systemd/systemd/issues/12997#issuecomment-510103988
2019-07-17 12:57:20 +02:00
Zbigniew Jędrzejewski-Szmek 62c6bbbc09 tree-wide: use PROJECT_FILE instead of __FILE__
This replaces the internal uses of __FILE__ with the new macro.
2019-07-04 10:36:00 +02:00
Ben Boeckel 5238e95759 codespell: fix spelling errors 2019-04-29 16:47:18 +02:00
Lennart Poettering cbb3092ce6 json: let's not accept embedded NUL bytes when allocating JSON strings
Let's add an additional paranoia check, and not accept embedded NUL
bytes in strings, just in case.
2019-04-26 08:24:41 +02:00
Lennart Poettering 1b266e3c6f json: be more careful when iterating through a JSON object/array
Let's exit the loop early in case the variant is not actually an object
or array. This is safer since otherwise we might end up iterating
through these variants and access fields that aren't of the type we
expect them to be and then bad things happen.

Of course, this doesn't absolve uses of these macros to check the type
of the variant explicitly beforehand, but it makes it less bad if they
forget to do so.
2019-04-12 13:11:11 +02:00
Lennart Poettering 33d60b8d57 json: simplify JSON_VARIANT_OBJECT_FOREACH() macro a bit
There's no point in returning the "key" within each loop iteration as
JsonVariant object. Let's simplify things and return it as string. That
simplifies usage (since the caller doesn't have to convert the object to
the string anymore) and is safe since we already validate that keys are
strings when an object JsonVariant is allocated.
2019-04-12 13:11:11 +02:00
Zbigniew Jędrzejewski-Szmek ca78ad1de9 headers: remove unneeded includes from util.h
This means we need to include many more headers in various files that simply
included util.h before, but it seems cleaner to do it this way.
2019-03-27 11:53:12 +01:00
Zbigniew Jędrzejewski-Szmek 19130626a0 nspawn-oci: use SYNTHETIC_ERRNO 2019-03-21 10:51:43 +01:00
Lennart Poettering 6af022fedd json: fix typo 2019-01-07 17:50:39 +01:00
Lennart Poettering fc0f6fbffc json: teach json_log() the new SYNTHETIC_ERRNO() logic 2019-01-07 17:50:39 +01:00
Lennart Poettering ab91733c7e json: add new display flag JSON_FORMAT_COLOR_AUTO
Typically we want to enable color when colors_enabled() says so, hence
let's automatize this.
2018-12-03 22:42:38 +01:00
Lennart Poettering 897f099bb0 json: introduce explicit type for json formatting flags 2018-12-03 22:42:38 +01:00
Lennart Poettering 90b365cee0 json: use new _align_() macro 2018-12-03 13:28:26 +01:00
Lennart Poettering 319a4f27c4 json: teach json builder "conditional" object fields
Quite often when we generate objects some fields should only be
generated in some conditions. Let's add high-level support for that.
Matching the existing JSON_BUILD_PAIR() this adds
JSON_BUILD_PAIR_CONDITIONAL() which is very similar, but takes an
additional parameter: a boolean condition. If "true" this acts like
JSON_BUILD_PAIR(), but if false then the whole pair is suppressed.

This sounds simply, but requires a tiny bit of complexity: when complex
sub-variants are used in fields, then we also need to suppress them.
2018-11-28 08:38:55 +01:00
Zbigniew Jędrzejewski-Szmek d284b82b3e Move various files that don't need to be in basic/ to shared/
This doesn't have much effect on the final build, because we link libbasic.a
into libsystemd-shared.so, so in the end, all the object built from basic/
end up in libsystemd-shared. And when the static library is linked into binaries,
any objects that are included in it but are not used are trimmed. Hence, the
size of output artifacts doesn't change:

$ du -sb /var/tmp/inst*
54181861	/var/tmp/inst1    (old)
54207441	/var/tmp/inst1s   (old split-usr)
54182477	/var/tmp/inst2    (new)
54208041	/var/tmp/inst2s   (new split-usr)

(The negligible change in size is because libsystemd-shared.so is bigger
by a few hundred bytes. I guess it's because symbols are named differently
or something like that.)

The effect is on the build process, in particular partial builds. This change
effectively moves the requirements on some build steps toward the leaves of the
dependency tree. Two effects:
- when building items that do not depend on libsystemd-shared, we
  build less stuff for libbasic.a (which wouldn't be used anyway,
  so it's a net win).
- when building items that do depend on libshared, we reduce libbasic.a as a
  synchronization point, possibly allowing better parallelism.

Method:
1. copy list of .h files from src/basic/meson.build to /tmp/basic
2. $ for i in $(grep '.h$' /tmp/basic); do echo $i; git --no-pager grep "include \"$i\"" src/basic/ 'src/lib*' 'src/nss-*' 'src/journal/sd-journal.c' |grep -v "${i%.h}.c";echo ;done | less
2018-11-20 07:27:37 +01:00
Kay Sievers a095315b3c build-sys: split internal basic/ library from shared/
basic/      can be used by everything
            cannot use anything outside of basic/

libsystemd/ can use basic/
            cannot use shared/

shared/     can use libsystemd/
2015-06-11 10:52:46 +02:00
Lennart Poettering dde8bb32b1 json: minor style fixes 2015-05-21 23:30:37 +02:00
Pavel Odvody ed967b12be shared/json: Added DOM-like JSON parser
This makes working with complexly structured documents easy
and more reliable as the parser is not susceptible to
element re-ordering.

Also fixes a bug when the tokenizer would choke after reading
a number.
2015-05-19 18:21:48 +02:00
Thomas Hindoe Paaboel Andersen 2eec67acbb remove unused includes
This patch removes includes that are not used. The removals were found with
include-what-you-use which checks if any of the symbols from a header is
in use.
2015-02-23 23:53:42 +01:00