| 1 |
/* |
| 2 |
* Copyright (C) 2020 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "config.h" |
| 7 |
|
| 8 |
#include "cockpitjsonprint.h" |
| 9 |
|
| 10 |
#include <assert.h> |
| 11 |
#include <errno.h> |
| 12 |
#include <fcntl.h> |
| 13 |
#include <inttypes.h> |
| 14 |
#include <string.h> |
| 15 |
#include <sys/mman.h> |
| 16 |
|
| 17 |
static bool |
| 18 |
char_needs_json_escape (unsigned char c) |
| 19 |
{ |
| 20 |
/* we escape: |
| 21 |
* - ascii controls |
| 22 |
* - backslash |
| 23 |
* - double quote |
| 24 |
* - ascii del |
| 25 |
* - all non-ascii |
| 26 |
*/ |
| 27 |
return c < ' ' || c == '\\' || c == '"' || c >= 0x7f; |
| 28 |
} |
| 29 |
|
| 30 |
static bool |
| 31 |
json_escape_char (FILE *stream, |
| 32 |
unsigned char c) |
| 33 |
{ |
| 34 |
if (c == '\\') |
| 35 |
return fputs ("\\\\", stream) >= 0; |
| 36 |
else if (c == '"') |
| 37 |
return fputs ("\\\"", stream) >= 0; |
| 38 |
else if (c >= 0x80) /* non-ascii */ |
| 39 |
return fputc ('?', stream) >= 0; |
| 40 |
else |
| 41 |
return fprintf (stream, "\\u%04x", c) == 6; |
| 42 |
} |
| 43 |
|
| 44 |
static bool |
| 45 |
json_escape_string (FILE *stream, |
| 46 |
const char *str, |
| 47 |
size_t maxlen) |
| 48 |
{ |
| 49 |
size_t offset = 0; |
| 50 |
|
| 51 |
while (offset < maxlen && str[offset]) |
| 52 |
{ |
| 53 |
size_t start = offset; |
| 54 |
|
| 55 |
while (offset < maxlen && str[offset] && !char_needs_json_escape (str[offset])) |
| 56 |
offset++; |
| 57 |
|
| 58 |
/* print the non-escaped prefix, if there is one */ |
| 59 |
if (offset != start) |
| 60 |
{ |
| 61 |
size_t length = offset - start; |
| 62 |
if (fwrite (str + start, 1, length, stream) != length) |
| 63 |
return false; |
| 64 |
} |
| 65 |
|
| 66 |
/* print the escaped character, if there is one */ |
| 67 |
if (offset < maxlen && str[offset]) |
| 68 |
{ |
| 69 |
if (!json_escape_char (stream, str[offset])) |
| 70 |
return false; |
| 71 |
|
| 72 |
offset++; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
return true; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* cockpit_json_print_string_property: |
| 81 |
* @stream: a stdio stream open for writing |
| 82 |
* @key: the JSON key name |
| 83 |
* @value: the string value to write |
| 84 |
* @maxlen: the maximum length of @value |
| 85 |
* |
| 86 |
* Adds a string key/value pair to a JSON object. |
| 87 |
* |
| 88 |
* @key and @value should both be plain ASCII. @key is copied directly |
| 89 |
* to the stream and must not contain any characters that would require |
| 90 |
* escapes. @value is escaped, if necessary (including replacing |
| 91 |
* non-ASCII characters with '?'). |
| 92 |
* |
| 93 |
* @maxlen can be -1 if @value is nul-terminated. Otherwise, @maxlen is |
| 94 |
* a maximum: the actual number of characters escaped and written is the |
| 95 |
* lesser of the length of the string or @maxlen. |
| 96 |
* |
| 97 |
* Returns true if the value was correctly written. |
| 98 |
*/ |
| 99 |
bool |
| 100 |
cockpit_json_print_string_property (FILE *stream, |
| 101 |
const char *key, |
| 102 |
const char *value, |
| 103 |
ssize_t maxlen) |
| 104 |
{ |
| 105 |
size_t expected = strlen (key) + 7; |
| 106 |
|
| 107 |
return fprintf (stream, ", \"%s\": \"", key) == expected && |
| 108 |
json_escape_string (stream, value, maxlen) && |
| 109 |
fputc ('"', stream) >= 0; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* cockpit_json_print_bool_property: |
| 114 |
* @stream: a stdio stream open for writing |
| 115 |
* @key: the JSON key name |
| 116 |
* @value: the boolean value to write |
| 117 |
* |
| 118 |
* Adds a boolean key/value pair to a JSON object. The boolean value is |
| 119 |
* formatted as either the string "true" or "false". |
| 120 |
* |
| 121 |
* Returns true if the value was correctly written. |
| 122 |
*/ |
| 123 |
bool |
| 124 |
cockpit_json_print_bool_property (FILE *stream, |
| 125 |
const char *key, |
| 126 |
bool value) |
| 127 |
{ |
| 128 |
size_t expected = 6 + strlen (key) + (value ? 4 : 5); /* "true" or "false" */ |
| 129 |
|
| 130 |
return fprintf (stream, ", \"%s\": %s", key, value ? "true" : "false") == expected; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* cockpit_json_print_integer_property: |
| 135 |
* @stream: a stdio stream open for writing |
| 136 |
* @key: the JSON key name |
| 137 |
* @value: the unsigned integer value to write |
| 138 |
* |
| 139 |
* Adds an integer key/value pair to a JSON object. |
| 140 |
* |
| 141 |
* Returns true if the value was correctly written. |
| 142 |
*/ |
| 143 |
bool |
| 144 |
cockpit_json_print_integer_property (FILE *stream, |
| 145 |
const char *key, |
| 146 |
uint64_t value) |
| 147 |
{ |
| 148 |
/* too much effort to figure out the expected length exactly */ |
| 149 |
return fprintf (stream, ", \"%s\": %"PRIu64, key, value) > 6; |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* cockpit_json_print_open_memfd: |
| 154 |
* @name: passed to memfd_create, gets displayed in /proc/.../fd |
| 155 |
* @version: if not negative then a "version" field will be added |
| 156 |
* |
| 157 |
* Creates a memfd, wraps it in a stdio stream, and starts the printing |
| 158 |
* of a JSON object into it by writing a '{' character and an optional |
| 159 |
* version field. |
| 160 |
* |
| 161 |
* If you don't write the version field, you need to take care to write |
| 162 |
* something else before first using the other cockpit_json_print_* |
| 163 |
* functions, because they all prepend commas. |
| 164 |
* |
| 165 |
* This function always returns a valid stream. In case of any errors, |
| 166 |
* the program is aborted. |
| 167 |
*/ |
| 168 |
FILE * |
| 169 |
cockpit_json_print_open_memfd (const char *name, |
| 170 |
int version) |
| 171 |
{ |
| 172 |
int fd; |
| 173 |
/* current kernels moan about not specifying exec mode */ |
| 174 |
#ifdef MFD_NOEXEC_SEAL |
| 175 |
fd = memfd_create (name, MFD_ALLOW_SEALING | MFD_CLOEXEC | MFD_NOEXEC_SEAL); |
| 176 |
/* fallback for older kernels */ |
| 177 |
if (fd == -1 && errno == EINVAL) |
| 178 |
#endif |
| 179 |
fd = memfd_create (name, MFD_ALLOW_SEALING | MFD_CLOEXEC); |
| 180 |
assert (fd != -1); |
| 181 |
|
| 182 |
FILE *stream = fdopen (fd, "w"); |
| 183 |
assert (stream != NULL); |
| 184 |
|
| 185 |
if (version >= 0) |
| 186 |
fprintf (stream, "{\"version\": %d", version); |
| 187 |
else |
| 188 |
fputc ('{', stream); |
| 189 |
|
| 190 |
return stream; |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* cockpit_json_print_finish_memfd: |
| 195 |
* @stream: the pointer to where a stream created by |
| 196 |
* cockpit_json_print_open_memfd() is stored. |
| 197 |
* |
| 198 |
* Finishes off the printing of a JSON object to a memfd by writing |
| 199 |
* the closing '}', sealing the memfd, and reopening it readonly. |
| 200 |
* |
| 201 |
* @stream is closed, and set to %NULL. |
| 202 |
* |
| 203 |
* This function always returns a valid readonly file descriptor |
| 204 |
* pointing to the sealed memfd. In case of any errors, the program is |
| 205 |
* aborted. |
| 206 |
*/ |
| 207 |
int |
| 208 |
cockpit_json_print_finish_memfd (FILE **stream) |
| 209 |
{ |
| 210 |
int r = fputc ('}', *stream); |
| 211 |
assert (r == '}'); |
| 212 |
|
| 213 |
r = fflush (*stream); |
| 214 |
assert (r == 0); |
| 215 |
|
| 216 |
int fd = fileno (*stream); |
| 217 |
|
| 218 |
const int seals = F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_WRITE; |
| 219 |
r = fcntl (fd, F_ADD_SEALS, seals); |
| 220 |
assert (r == 0); |
| 221 |
|
| 222 |
char fd_name[] = "/proc/self/fd/xxxxxx"; |
| 223 |
r = snprintf (fd_name, sizeof fd_name, "/proc/self/fd/%d", fd); |
| 224 |
assert (r < sizeof fd_name); |
| 225 |
|
| 226 |
int readonly_fd = open (fd_name, O_RDONLY); |
| 227 |
assert (readonly_fd != -1); |
| 228 |
|
| 229 |
fclose (*stream); |
| 230 |
*stream = NULL; |
| 231 |
|
| 232 |
return readonly_fd; |
| 233 |
} |