| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <unistd.h> |
| 4 |
|
| 5 |
#include "sd-bus.h" |
| 6 |
|
| 7 |
#include "alloc-util.h" |
| 8 |
#include "bus-error.h" |
| 9 |
#include "bus-locator.h" |
| 10 |
#include "bus-util.h" |
| 11 |
#include "env-util.h" |
| 12 |
#include "escape.h" |
| 13 |
#include "log.h" |
| 14 |
#include "output-mode.h" |
| 15 |
#include "pager.h" |
| 16 |
#include "string-util.h" |
| 17 |
#include "strv.h" |
| 18 |
#include "systemctl.h" |
| 19 |
#include "systemctl-set-environment.h" |
| 20 |
#include "systemctl-util.h" |
| 21 |
|
| 22 |
static int json_transform_message(sd_bus_message *m, sd_json_variant **ret) { |
| 23 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; |
| 24 |
const char *text; |
| 25 |
int r; |
| 26 |
|
| 27 |
assert(m); |
| 28 |
assert(ret); |
| 29 |
|
| 30 |
while ((r = sd_bus_message_read_basic(m, SD_BUS_TYPE_STRING, &text)) > 0) { |
| 31 |
_cleanup_free_ char *n = NULL; |
| 32 |
const char *sep; |
| 33 |
|
| 34 |
sep = strchr(text, '='); |
| 35 |
if (!sep) |
| 36 |
return log_error_errno(SYNTHETIC_ERRNO(EUCLEAN), |
| 37 |
"Invalid environment block"); |
| 38 |
|
| 39 |
n = strndup(text, sep - text); |
| 40 |
if (!n) |
| 41 |
return log_oom(); |
| 42 |
|
| 43 |
sep++; |
| 44 |
|
| 45 |
r = sd_json_variant_set_field_string(&v, n, sep); |
| 46 |
if (r < 0) |
| 47 |
return log_error_errno(r, "Failed to set JSON field '%s' to '%s': %m", n, sep); |
| 48 |
} |
| 49 |
if (r < 0) |
| 50 |
return bus_log_parse_error(r); |
| 51 |
|
| 52 |
*ret = TAKE_PTR(v); |
| 53 |
return 0; |
| 54 |
} |
| 55 |
|
| 56 |
static int print_variable(const char *s) { |
| 57 |
const char *sep; |
| 58 |
_cleanup_free_ char *esc = NULL; |
| 59 |
|
| 60 |
sep = strchr(s, '='); |
| 61 |
if (!sep) |
| 62 |
return log_error_errno(SYNTHETIC_ERRNO(EUCLEAN), |
| 63 |
"Invalid environment block"); |
| 64 |
|
| 65 |
esc = shell_maybe_quote(sep + 1, SHELL_ESCAPE_POSIX); |
| 66 |
if (!esc) |
| 67 |
return log_oom(); |
| 68 |
|
| 69 |
printf("%.*s=%s\n", (int)(sep-s), s, esc); |
| 70 |
return 0; |
| 71 |
} |
| 72 |
|
| 73 |
int verb_show_environment(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 74 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 75 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
| 76 |
const char *text; |
| 77 |
sd_bus *bus; |
| 78 |
int r; |
| 79 |
|
| 80 |
r = acquire_bus(BUS_MANAGER, &bus); |
| 81 |
if (r < 0) |
| 82 |
return r; |
| 83 |
|
| 84 |
pager_open(arg_pager_flags); |
| 85 |
|
| 86 |
r = bus_get_property(bus, bus_systemd_mgr, "Environment", &error, &reply, "as"); |
| 87 |
if (r < 0) |
| 88 |
return log_error_errno(r, "Failed to get environment: %s", bus_error_message(&error, r)); |
| 89 |
|
| 90 |
r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "s"); |
| 91 |
if (r < 0) |
| 92 |
return bus_log_parse_error(r); |
| 93 |
|
| 94 |
if (OUTPUT_MODE_IS_JSON(arg_output)) { |
| 95 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; |
| 96 |
|
| 97 |
r = json_transform_message(reply, &v); |
| 98 |
if (r < 0) |
| 99 |
return r; |
| 100 |
|
| 101 |
sd_json_variant_dump(v, output_mode_to_json_format_flags(arg_output), stdout, NULL); |
| 102 |
} else { |
| 103 |
while ((r = sd_bus_message_read_basic(reply, SD_BUS_TYPE_STRING, &text)) > 0) { |
| 104 |
r = print_variable(text); |
| 105 |
if (r < 0) |
| 106 |
return r; |
| 107 |
} |
| 108 |
if (r < 0) |
| 109 |
return bus_log_parse_error(r); |
| 110 |
} |
| 111 |
|
| 112 |
r = sd_bus_message_exit_container(reply); |
| 113 |
if (r < 0) |
| 114 |
return bus_log_parse_error(r); |
| 115 |
|
| 116 |
return 0; |
| 117 |
} |
| 118 |
|
| 119 |
static void invalid_callback(const char *p, void *userdata) { |
| 120 |
_cleanup_free_ char *t = cescape(p); |
| 121 |
|
| 122 |
log_debug("Ignoring invalid environment assignment \"%s\".", strnull(t)); |
| 123 |
} |
| 124 |
|
| 125 |
int verb_set_environment(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 126 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 127 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
| 128 |
const char *method; |
| 129 |
sd_bus *bus; |
| 130 |
int r; |
| 131 |
|
| 132 |
assert(argc > 1); |
| 133 |
assert(argv); |
| 134 |
|
| 135 |
r = acquire_bus(BUS_MANAGER, &bus); |
| 136 |
if (r < 0) |
| 137 |
return r; |
| 138 |
|
| 139 |
polkit_agent_open_maybe(); |
| 140 |
|
| 141 |
method = streq(argv[0], "set-environment") |
| 142 |
? "SetEnvironment" |
| 143 |
: "UnsetEnvironment"; |
| 144 |
|
| 145 |
r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, method); |
| 146 |
if (r < 0) |
| 147 |
return bus_log_create_error(r); |
| 148 |
|
| 149 |
r = sd_bus_message_append_strv(m, strv_skip(argv, 1)); |
| 150 |
if (r < 0) |
| 151 |
return bus_log_create_error(r); |
| 152 |
|
| 153 |
r = sd_bus_call(bus, m, 0, &error, NULL); |
| 154 |
if (r < 0) |
| 155 |
return log_error_errno(r, "Failed to set environment: %s", bus_error_message(&error, r)); |
| 156 |
|
| 157 |
return 0; |
| 158 |
} |
| 159 |
|
| 160 |
int verb_import_environment(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 161 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 162 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
| 163 |
sd_bus *bus; |
| 164 |
int r; |
| 165 |
|
| 166 |
r = acquire_bus(BUS_MANAGER, &bus); |
| 167 |
if (r < 0) |
| 168 |
return r; |
| 169 |
|
| 170 |
polkit_agent_open_maybe(); |
| 171 |
|
| 172 |
r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, "SetEnvironment"); |
| 173 |
if (r < 0) |
| 174 |
return bus_log_create_error(r); |
| 175 |
|
| 176 |
if (argc < 2) { |
| 177 |
log_warning("Calling import-environment without a list of variable names is deprecated."); |
| 178 |
|
| 179 |
_cleanup_strv_free_ char **copy = strv_copy(environ); |
| 180 |
if (!copy) |
| 181 |
return log_oom(); |
| 182 |
|
| 183 |
strv_env_clean_with_callback(copy, invalid_callback, NULL); |
| 184 |
|
| 185 |
STRV_FOREACH(e, copy) |
| 186 |
if (string_has_cc(*e, NULL)) |
| 187 |
log_notice("Environment variable $%.*s contains control characters, importing anyway.", |
| 188 |
(int) strcspn(*e, "="), *e); |
| 189 |
|
| 190 |
r = sd_bus_message_append_strv(m, copy); |
| 191 |
|
| 192 |
} else { |
| 193 |
r = sd_bus_message_open_container(m, 'a', "s"); |
| 194 |
if (r < 0) |
| 195 |
return bus_log_create_error(r); |
| 196 |
|
| 197 |
STRV_FOREACH(a, strv_skip(argv, 1)) { |
| 198 |
|
| 199 |
if (!env_name_is_valid(*a)) |
| 200 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 201 |
"Not a valid environment variable name: %s", *a); |
| 202 |
|
| 203 |
bool found = false; |
| 204 |
STRV_FOREACH(b, environ) { |
| 205 |
const char *eq; |
| 206 |
|
| 207 |
eq = startswith(*b, *a); |
| 208 |
if (eq && *eq == '=') { |
| 209 |
if (string_has_cc(eq + 1, NULL)) |
| 210 |
log_notice("Environment variable $%.*s contains control characters, importing anyway.", |
| 211 |
(int) (eq - *b), *b); |
| 212 |
|
| 213 |
r = sd_bus_message_append(m, "s", *b); |
| 214 |
if (r < 0) |
| 215 |
return bus_log_create_error(r); |
| 216 |
|
| 217 |
found = true; |
| 218 |
break; |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
if (!found) |
| 223 |
log_notice("Environment variable $%s not set, ignoring.", *a); |
| 224 |
} |
| 225 |
|
| 226 |
r = sd_bus_message_close_container(m); |
| 227 |
} |
| 228 |
if (r < 0) |
| 229 |
return bus_log_create_error(r); |
| 230 |
|
| 231 |
r = sd_bus_call(bus, m, 0, &error, NULL); |
| 232 |
if (r < 0) |
| 233 |
return log_error_errno(r, "Failed to import environment: %s", bus_error_message(&error, r)); |
| 234 |
|
| 235 |
return 0; |
| 236 |
} |