| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <fcntl.h> |
| 4 |
#include <unistd.h> |
| 5 |
|
| 6 |
#include "alloc-util.h" |
| 7 |
#include "creds-util.h" |
| 8 |
#include "extract-word.h" |
| 9 |
#include "fd-util.h" |
| 10 |
#include "fileio.h" |
| 11 |
#include "generator.h" |
| 12 |
#include "initrd-util.h" |
| 13 |
#include "log.h" |
| 14 |
#include "parse-util.h" |
| 15 |
#include "path-util.h" |
| 16 |
#include "proc-cmdline.h" |
| 17 |
#include "process-util.h" |
| 18 |
#include "string-util.h" |
| 19 |
#include "strv.h" |
| 20 |
#include "terminal-util.h" |
| 21 |
#include "unit-name.h" |
| 22 |
#include "virt.h" |
| 23 |
|
| 24 |
typedef enum { |
| 25 |
GETTY_SOURCE_NONE = 0, |
| 26 |
GETTY_SOURCE_CREDENTIAL = 1 << 0, |
| 27 |
GETTY_SOURCE_CONTAINER = 1 << 1, |
| 28 |
GETTY_SOURCE_CONSOLE = 1 << 2, |
| 29 |
GETTY_SOURCE_BUILTIN = 1 << 3, |
| 30 |
GETTY_SOURCE_ALL = GETTY_SOURCE_CREDENTIAL | GETTY_SOURCE_CONTAINER | GETTY_SOURCE_CONSOLE | GETTY_SOURCE_BUILTIN, |
| 31 |
} GettySourceFlag; |
| 32 |
|
| 33 |
static const char *arg_dest = NULL; |
| 34 |
static GettySourceFlag arg_getty_sources = GETTY_SOURCE_ALL; |
| 35 |
|
| 36 |
static int add_getty_impl(const char *tty, const char *path, const char *type, const char *unit_path) { |
| 37 |
int r; |
| 38 |
|
| 39 |
assert(type); |
| 40 |
assert(path); |
| 41 |
assert(unit_path); |
| 42 |
|
| 43 |
if (!filename_is_valid(tty)) { |
| 44 |
log_debug("Invalid %s tty device specified, ignoring: %s", type, path); |
| 45 |
return 0; |
| 46 |
} |
| 47 |
|
| 48 |
_cleanup_free_ char *instance = NULL; |
| 49 |
r = unit_name_path_escape(tty, &instance); |
| 50 |
if (r < 0) |
| 51 |
return log_error_errno(r, "Failed to escape %s tty path %s: %m", type, tty); |
| 52 |
|
| 53 |
log_debug("Automatically adding %s getty for %s.", type, tty); |
| 54 |
|
| 55 |
return generator_add_symlink_full(arg_dest, "getty.target", "wants", unit_path, instance); |
| 56 |
} |
| 57 |
|
| 58 |
static int add_serial_getty(const char *path) { |
| 59 |
const char *tty = skip_dev_prefix(ASSERT_PTR(path)); |
| 60 |
return add_getty_impl(tty, path, "serial", SYSTEM_DATA_UNIT_DIR "/[email protected]"); |
| 61 |
} |
| 62 |
|
| 63 |
static int add_container_getty(const char *tty_or_path) { |
| 64 |
const char *tty; |
| 65 |
|
| 66 |
assert(tty_or_path); |
| 67 |
|
| 68 |
if (is_path(tty_or_path)) |
| 69 |
/* Check if it is actually a pty. */ |
| 70 |
tty = path_startswith(skip_dev_prefix(tty_or_path), "pts/"); |
| 71 |
else |
| 72 |
tty = tty_or_path; |
| 73 |
|
| 74 |
return add_getty_impl(tty, tty_or_path, "container", SYSTEM_DATA_UNIT_DIR "/[email protected]"); |
| 75 |
} |
| 76 |
|
| 77 |
static int verify_tty(const char *path) { |
| 78 |
_cleanup_close_ int fd = -EBADF; |
| 79 |
|
| 80 |
assert(path); |
| 81 |
|
| 82 |
/* Some TTYs are weird and have been enumerated but don't work when you try to use them, such as |
| 83 |
* classic ttyS0 and friends. Let's check that and open the device and run isatty() on it. */ |
| 84 |
|
| 85 |
/* O_NONBLOCK is essential here, to make sure we don't wait for DCD */ |
| 86 |
fd = open(path, O_RDWR|O_NONBLOCK|O_NOCTTY|O_CLOEXEC|O_NOFOLLOW); |
| 87 |
if (fd < 0) |
| 88 |
return -errno; |
| 89 |
|
| 90 |
if (!isatty_safe(fd)) |
| 91 |
return -ENOTTY; |
| 92 |
|
| 93 |
return 0; |
| 94 |
} |
| 95 |
|
| 96 |
static int run_container(void) { |
| 97 |
int r; |
| 98 |
|
| 99 |
log_debug("Automatically adding console shell."); |
| 100 |
|
| 101 |
r = generator_add_symlink(arg_dest, "getty.target", "wants", SYSTEM_DATA_UNIT_DIR "/console-getty.service"); |
| 102 |
if (r < 0) |
| 103 |
return r; |
| 104 |
|
| 105 |
/* When $container_ttys is set for PID 1, spawn gettys on all ptys named therein. |
| 106 |
* Note that despite the variable name we only support ptys here. */ |
| 107 |
_cleanup_free_ char *container_ttys = NULL; |
| 108 |
(void) getenv_for_pid(1, "container_ttys", &container_ttys); |
| 109 |
|
| 110 |
for (const char *p = container_ttys;;) { |
| 111 |
_cleanup_free_ char *word = NULL; |
| 112 |
|
| 113 |
r = extract_first_word(&p, &word, NULL, 0); |
| 114 |
if (r < 0) |
| 115 |
return log_error_errno(r, "Failed to parse $container_ttys: %m"); |
| 116 |
if (r == 0) |
| 117 |
return 0; |
| 118 |
|
| 119 |
/* add_container_getty() also accepts a filename, but here we request that the string |
| 120 |
* contains "pts/". */ |
| 121 |
if (!is_path(word)) |
| 122 |
continue; |
| 123 |
|
| 124 |
r = add_container_getty(word); |
| 125 |
if (r < 0) |
| 126 |
return r; |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
static int add_credential_gettys(void) { |
| 131 |
static const struct { |
| 132 |
const char *credential_name; |
| 133 |
int (*func)(const char *tty); |
| 134 |
} table[] = { |
| 135 |
{ "getty.ttys.serial", add_serial_getty }, |
| 136 |
{ "getty.ttys.container", add_container_getty }, |
| 137 |
}; |
| 138 |
int r; |
| 139 |
|
| 140 |
log_debug("Checking credentials for configuration..."); |
| 141 |
|
| 142 |
FOREACH_ELEMENT(t, table) { |
| 143 |
_cleanup_free_ char *b = NULL; |
| 144 |
size_t sz = 0; |
| 145 |
|
| 146 |
r = read_credential_with_decryption(t->credential_name, (void*) &b, &sz); |
| 147 |
if (r < 0) |
| 148 |
return r; |
| 149 |
if (r == 0) |
| 150 |
continue; |
| 151 |
|
| 152 |
_cleanup_fclose_ FILE *f = NULL; |
| 153 |
f = fmemopen_unlocked(b, sz, "r"); |
| 154 |
if (!f) |
| 155 |
return log_oom(); |
| 156 |
|
| 157 |
for (;;) { |
| 158 |
_cleanup_free_ char *tty = NULL; |
| 159 |
|
| 160 |
r = read_stripped_line(f, PATH_MAX, &tty); |
| 161 |
if (r == 0) |
| 162 |
break; |
| 163 |
if (r < 0) { |
| 164 |
log_error_errno(r, "Failed to parse credential %s: %m", t->credential_name); |
| 165 |
break; |
| 166 |
} |
| 167 |
|
| 168 |
if (startswith(tty, "#")) |
| 169 |
continue; |
| 170 |
|
| 171 |
r = t->func(tty); |
| 172 |
if (r < 0) |
| 173 |
return r; |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
return 0; |
| 178 |
} |
| 179 |
|
| 180 |
static int parse_getty_sources(const char *s, GettySourceFlag *ret) { |
| 181 |
int r; |
| 182 |
|
| 183 |
assert(ret); |
| 184 |
|
| 185 |
if (isempty(s)) { |
| 186 |
*ret = GETTY_SOURCE_ALL; |
| 187 |
return 0; |
| 188 |
} |
| 189 |
|
| 190 |
r = parse_boolean(s); |
| 191 |
if (r >= 0) { |
| 192 |
*ret = r ? GETTY_SOURCE_ALL : GETTY_SOURCE_NONE; |
| 193 |
return 0; |
| 194 |
} |
| 195 |
|
| 196 |
static struct { |
| 197 |
GettySourceFlag flag; |
| 198 |
const char *str; |
| 199 |
} table[] = { |
| 200 |
{ GETTY_SOURCE_CREDENTIAL, "credential", }, |
| 201 |
{ GETTY_SOURCE_CONTAINER, "container", }, |
| 202 |
{ GETTY_SOURCE_CONSOLE, "console", }, |
| 203 |
{ GETTY_SOURCE_BUILTIN, "builtin", }, |
| 204 |
}; |
| 205 |
|
| 206 |
GettySourceFlag flags = 0; |
| 207 |
for (const char *p = s;;) { |
| 208 |
_cleanup_free_ char *word = NULL; |
| 209 |
|
| 210 |
r = extract_first_word(&p, &word, ",", /* flags= */ 0); |
| 211 |
if (r < 0) |
| 212 |
return r; |
| 213 |
if (r == 0) |
| 214 |
break; |
| 215 |
|
| 216 |
bool found = false; |
| 217 |
FOREACH_ELEMENT(i, table) |
| 218 |
if (streq(word, i->str)) { |
| 219 |
flags |= i->flag; |
| 220 |
found = true; |
| 221 |
break; |
| 222 |
} |
| 223 |
|
| 224 |
if (!found) |
| 225 |
return -EINVAL; |
| 226 |
} |
| 227 |
|
| 228 |
*ret = flags; |
| 229 |
return 0; |
| 230 |
} |
| 231 |
|
| 232 |
static int parse_proc_cmdline_item(const char *key, const char *value, void *data) { |
| 233 |
int r; |
| 234 |
|
| 235 |
assert(key); |
| 236 |
|
| 237 |
if (proc_cmdline_key_streq(key, "systemd.getty_auto")) { |
| 238 |
r = parse_getty_sources(value, &arg_getty_sources); |
| 239 |
if (r < 0) |
| 240 |
log_warning_errno(r, "Failed to parse systemd.getty_auto= kernel command line option, ignoring: %s", value); |
| 241 |
} |
| 242 |
|
| 243 |
return 0; |
| 244 |
} |
| 245 |
|
| 246 |
static void parse_env(void) { |
| 247 |
_cleanup_free_ char *value = NULL; |
| 248 |
int r; |
| 249 |
|
| 250 |
r = getenv_for_pid(1, "SYSTEMD_GETTY_AUTO", &value); |
| 251 |
if (r < 0) |
| 252 |
log_warning_errno(r, "Failed to read $SYSTEMD_GETTY_AUTO in PID 1's environment, ignoring: %m"); |
| 253 |
else if (r > 0) { |
| 254 |
r = parse_getty_sources(value, &arg_getty_sources); |
| 255 |
if (r < 0) |
| 256 |
log_warning_errno(r, "Failed to parse $SYSTEMD_GETTY_AUTO from PID 1's environment, ignoring: %s", |
| 257 |
value); |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
static void parse_credentials(void) { |
| 262 |
_cleanup_free_ char *value = NULL; |
| 263 |
int r; |
| 264 |
|
| 265 |
r = read_credential_with_decryption("getty.auto", (void**) &value, /* ret_size= */ NULL); |
| 266 |
if (r < 0) |
| 267 |
log_debug_errno(r, "Failed to read credential 'getty.auto', ignoring: %m"); |
| 268 |
else if (r > 0) { |
| 269 |
r = parse_getty_sources(value, &arg_getty_sources); |
| 270 |
if (r < 0) |
| 271 |
log_warning_errno(r, "Invalid 'getty.auto' credential, ignoring: %s", value); |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
static int run(const char *dest, const char *dest_early, const char *dest_late) { |
| 276 |
int r; |
| 277 |
|
| 278 |
assert_se(arg_dest = dest); |
| 279 |
|
| 280 |
if (in_initrd()) { |
| 281 |
log_debug("Skipping generator, running in the initrd."); |
| 282 |
return EXIT_SUCCESS; |
| 283 |
} |
| 284 |
|
| 285 |
r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, 0); |
| 286 |
if (r < 0) |
| 287 |
log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m"); |
| 288 |
|
| 289 |
parse_env(); |
| 290 |
parse_credentials(); |
| 291 |
|
| 292 |
if (arg_getty_sources == GETTY_SOURCE_NONE) { |
| 293 |
log_debug("Disabled, exiting."); |
| 294 |
return 0; |
| 295 |
} |
| 296 |
|
| 297 |
if (FLAGS_SET(arg_getty_sources, GETTY_SOURCE_CREDENTIAL)) { |
| 298 |
r = add_credential_gettys(); |
| 299 |
if (r < 0) |
| 300 |
return r; |
| 301 |
} |
| 302 |
|
| 303 |
if (detect_container() > 0) { |
| 304 |
/* Add console shell and look at $container_ttys, but don't do add any |
| 305 |
* further magic if we are in a container. */ |
| 306 |
if (FLAGS_SET(arg_getty_sources, GETTY_SOURCE_CONTAINER)) |
| 307 |
return run_container(); |
| 308 |
|
| 309 |
return 0; |
| 310 |
} |
| 311 |
|
| 312 |
/* Automatically add in a serial getty on all active kernel consoles */ |
| 313 |
if (FLAGS_SET(arg_getty_sources, GETTY_SOURCE_CONSOLE)) { |
| 314 |
_cleanup_strv_free_ char **consoles = NULL; |
| 315 |
r = get_kernel_consoles(&consoles); |
| 316 |
if (r < 0) |
| 317 |
log_warning_errno(r, "Failed to get active kernel consoles, ignoring: %m"); |
| 318 |
else if (r > 0) |
| 319 |
STRV_FOREACH(i, consoles) { |
| 320 |
/* We assume that gettys on virtual terminals are started via manual |
| 321 |
* configuration and do this magic only for non-VC terminals. */ |
| 322 |
if (tty_is_vc(*i)) |
| 323 |
continue; |
| 324 |
|
| 325 |
if (verify_tty(*i) < 0) |
| 326 |
continue; |
| 327 |
|
| 328 |
r = add_serial_getty(*i); |
| 329 |
if (r < 0) |
| 330 |
return r; |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
/* Automatically add a serial getty to each available virtualizer console. */ |
| 335 |
if (FLAGS_SET(arg_getty_sources, GETTY_SOURCE_BUILTIN)) |
| 336 |
FOREACH_STRING(j, |
| 337 |
"hvc0", |
| 338 |
"xvc0", |
| 339 |
"hvsi0", |
| 340 |
"sclp_line0", |
| 341 |
"ttysclp0", |
| 342 |
"3270/tty1") { |
| 343 |
_cleanup_free_ char *p = NULL; |
| 344 |
|
| 345 |
p = path_join("/dev", j); |
| 346 |
if (!p) |
| 347 |
return log_oom(); |
| 348 |
if (access(p, F_OK) < 0) |
| 349 |
continue; |
| 350 |
|
| 351 |
r = add_serial_getty(j); |
| 352 |
if (r < 0) |
| 353 |
return r; |
| 354 |
} |
| 355 |
|
| 356 |
return 0; |
| 357 |
} |
| 358 |
|
| 359 |
DEFINE_MAIN_GENERATOR_FUNCTION(run); |