Commit Graph

76 Commits

Author SHA1 Message Date
Lennart Poettering b95a05d014 json: add generic cmdline parser for --json= switch 2021-01-09 17:36:11 +01:00
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
Lennart Poettering e4dde4e87d
Merge pull request #17702 from rnhmjoj/master
Extend $SYSTEMD_COLORS to switch colors mode
2020-12-16 19:26:40 +01:00
rnhmjoj 25e4608b8b
tree-wide: avoid direct use of color macros 2020-12-15 19:29:42 +01:00
Zbigniew Jędrzejewski-Szmek d401a5e411 json: log location also when there is no file
E.g. in nss-resolve it is still useful to print the location of the error:
src/test/test-nss.c:231: dlsym(0x0x1dc6fb0, _nss_resolve_gethostbyname2_r) → 0x0x7fdbfc53f626
(string):1:40: JSON field ifindex is out of bounds for an interface index.

I opted to use a partially duplicated if condition to avoid nesting. It's nice
to have the log calls vertically aligned. The compiler will optimize this nicely.
2020-12-10 13:00:02 +01:00
Lennart Poettering 986311c2da fileio: teach read_full_file_full() to read from offset/with maximum size 2020-12-01 14:17:47 +01:00
Yu Watanabe db9ecf0501 license: LGPL-2.1+ -> LGPL-2.1-or-later 2020-11-09 13:23:58 +09:00
Lennart Poettering d3dcf4e3b9 fileio: beef up READ_FULL_FILE_CONNECT_SOCKET to allow setting sender socket name
This beefs up the READ_FULL_FILE_CONNECT_SOCKET logic of
read_full_file_full() a bit: when used a sender socket name may be
specified. If specified as NULL behaviour is as before: the client
socket name is picked by the kernel. But if specified as non-NULL the
client can pick a socket name to use when connecting. This is useful to
communicate a minimal amount of metainformation from client to server,
outside of the transport payload.

Specifically, these beefs up the service credential logic to pass an
abstract AF_UNIX socket name as client socket name when connecting via
READ_FULL_FILE_CONNECT_SOCKET, that includes the requesting unit name
and the eventual credential name. This allows servers implementing the
trivial credential socket logic to distinguish clients: via a simple
getpeername() it can be determined which unit is requesting a
credential, and which credential specifically.

Example: with this patch in place, in a unit file "waldo.service" a
configuration line like the following:

    LoadCredential=foo:/run/quux/creds.sock

will result in a connection to the AF_UNIX socket /run/quux/creds.sock,
originating from an abstract namespace AF_UNIX socket:

    @$RANDOM/unit/waldo.service/foo

(The $RANDOM is replaced by some randomized string. This is included in
the socket name order to avoid namespace squatting issues: the abstract
socket namespace is open to unprivileged users after all, and care needs
to be taken not to use guessable names)

The services listening on the /run/quux/creds.sock socket may thus
easily retrieve the name of the unit the credential is requested for
plus the credential name, via a simpler getpeername(), discarding the
random preifx and the /unit/ string.

This logic uses "/" as separator between the fields, since both unit
names and credential names appear in the file system, and thus are
designed to use "/" as outer separators. Given that it's a good safe
choice to use as separators here, too avoid any conflicts.

This is a minimal patch only: the new logic is used only for the unit
file credential logic. For other places where we use
READ_FULL_FILE_CONNECT_SOCKET it is probably a good idea to use this
scheme too, but this should be done carefully in later patches, since
the socket names become API that way, and we should determine the right
amount of info to pass over.
2020-11-03 09:48:04 +01:00
Lennart Poettering 66032ef489 macro: introduce POINTER_MAX as define for (void*) -1
Just add a safer, prettier way to write (void*) -1, that doesn't rely on
two's complement, but uses the correct underlying C constructs.
2020-10-22 08:33:20 +09:00
Zbigniew Jędrzejewski-Szmek ea9afe0064 shared/json: reject non-utf-8 strings
JSON strings must be utf-8-clean. We also verify this in json_parse_string()
so we would reject a message with invalid utf-8 anyway.

It would probably be slightly cheaper to detect non-conformaning strings in
serialization, but then we'd have to fail serialization. By doing this early,
we give the caller a chance to handle the error nicely.

The test is adjusted to contain a valid utf-8 string after decoding of the
utf-32 encoding in json ("विवेकख्यातिरविप्लवा हानोपायः।", something about the
cessation of ignorance).
2020-09-01 16:48:40 +02:00
Zbigniew Jędrzejewski-Szmek e12b6e1951 json: split out string formatting to a separate function
It's complicated enough to deserve it's own function.

No functional change.
2020-09-01 10:48:10 +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 8e2fa6e223 json: turn off ubsan for json_variant_has_type()
Fixes: #15907
2020-05-26 09:01:26 +02:00
Lennart Poettering 6028d766d1 macro: introduce DISABLE_WARNING_TYPE_LIMITS and make use of it everywhere 2020-05-25 18:25:09 +02:00
Lennart Poettering 6a5b28def2 json: use our regular way to turn off compiler warnings 2020-05-25 18:23:50 +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
Zbigniew Jędrzejewski-Szmek 162392b75a tree-wide: spellcheck using codespell
Fixes #15436.
2020-04-16 18:00:40 +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
Mike Gilbert 19bb96759a userdb: allow dots in username
This prevents an error in pam_systemd when logging in.

sshd[2623165]: pam_unix(sshd:session): session opened for user tony.stark(uid=10001) by (uid=0)
sshd[2623165]: pam_systemd(sshd:session): Failed to get user record: Invalid argument

Bug: https://bugs.gentoo.org/708824
2020-02-27 08:47:52 +01:00
Lennart Poettering 898820edb5 json: lower maximum allowed recursion to 2K
Apparently 4K is too high still, let's halve it.

Fixes: #14396
2020-01-21 10:50:09 +01: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 3dd1b600b8 json: permit 'null' as a way to reset tri-states to default 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
Lennart Poettering 07737617a1 json: beef up strv parser to also accept a single string instead of an array of strings
Let's be permissive in what we accept and take a single string instead
of an array of strings, when a string is requested, too.
2019-12-02 09:47:00 +01:00
Yu Watanabe 455fa9610c tree-wide: drop string.h when string-util.h or friends are included 2019-11-04 00:30:32 +09:00
Lennart Poettering 2eb1c19881 json: always allocate at least sizeof(JsonVariant) bytes
ubsan apparently doesn't like us being too smart here. Given the
difference is just a few bytes, let's avoid the noise.

Fixes: #13020
2019-07-11 15:10:33 +02:00
Lennart Poettering ca5e7694bd json: use JSON_VARIANT_ARRAY_FOREACH() where we can 2019-07-04 12:35:35 +09:00
Zbigniew Jędrzejewski-Szmek 2a04712c19 shared/varlink: add missing terminator in json strings
Should finally fix oss-fuzz-14688.
8688c29b5a wasn't enough.

The buffer retrieved from memstream has the size that the same as the written
data. When we write do write(f, s, strlen(s)), then no terminating NUL is written,
and the buffer is not (necessarilly) a proper C string.
2019-05-30 18:45:06 +02:00
Lennart Poettering 6980b04fb6 json: fix minor memory leak on error path 2019-05-20 18:12:48 +02:00