• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemd / systemd / 15336122756

29 May 2025 09:26PM UTC coverage: 72.064% (+0.02%) from 72.04%
15336122756

push

github

yuwata
NEWS: fix typos

299688 of 415863 relevant lines covered (72.06%)

699722.54 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/ssh-generator/ssh-generator.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <unistd.h>
4

5
#include "alloc-util.h"
6
#include "creds-util.h"
7
#include "errno-util.h"
8
#include "fd-util.h"
9
#include "fileio.h"
10
#include "generator.h"
11
#include "install.h"
12
#include "log.h"
13
#include "missing_socket.h"
14
#include "parse-util.h"
15
#include "path-lookup.h"
16
#include "path-util.h"
17
#include "proc-cmdline.h"
18
#include "socket-netlink.h"
19
#include "socket-util.h"
20
#include "special.h"
21
#include "string-util.h"
22
#include "strv.h"
23
#include "virt.h"
24

25
/* A small generator binding potentially five or more SSH sockets:
26
 *
27
 *     1. Listen on AF_VSOCK port 22 if we run in a VM with AF_VSOCK enabled
28
 *     2. Listen on AF_UNIX socket /run/host/unix-export/ssh if we run in a container with /run/host/ support
29
 *     3. Listen on AF_UNIX socket /run/ssh-unix-local/socket (always)
30
 *     4. Listen on any socket specified via kernel command line option systemd.ssh_listen=
31
 *     5. Similar, but from system credential ssh.listen
32
 *
33
 * The first two provide a nice way for hosts to connect to containers and VMs they invoke via the usual SSH
34
 * logic, but without waiting for networking or suchlike. The third allows the same for local clients. */
35

36
static const char *arg_dest = NULL;
37
static bool arg_auto = true;
38
static char **arg_listen_extra = NULL;
39

40
static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
×
41
        int r;
×
42

43
        assert(key);
×
44

45
        if (proc_cmdline_key_streq(key, "systemd.ssh_auto")) {
×
46
                r = value ? parse_boolean(value) : 1;
×
47
                if (r < 0)
×
48
                        log_warning_errno(r, "Failed to parse systemd.ssh_auto switch \"%s\", ignoring: %m", value);
×
49
                else
50
                        arg_auto = r;
×
51

52
        } else if (proc_cmdline_key_streq(key, "systemd.ssh_listen")) {
×
53

54
                if (proc_cmdline_value_missing(key, value))
×
55
                        return 0;
×
56

57
                SocketAddress sa;
×
58
                r = socket_address_parse(&sa, value);
×
59
                if (r < 0)
×
60
                        log_warning_errno(r, "Failed to parse systemd.ssh_listen= expression, ignoring: %s", value);
×
61
                else {
62
                        _cleanup_free_ char *s = NULL;
×
63
                        r = socket_address_print(&sa, &s);
×
64
                        if (r < 0)
×
65
                                return log_error_errno(r, "Failed to format socket address: %m");
×
66

67
                        if (strv_consume(&arg_listen_extra, TAKE_PTR(s)) < 0)
×
68
                                return log_oom();
×
69
                }
70
        }
71

72
        return 0;
73
}
74

75
static int make_sshd_template_unit(
×
76
                const char *dest,
77
                const char *template,
78
                const char *sshd_binary,
79
                const char *found_sshd_template_service,
80
                char **generated_sshd_template_unit) {
81

82
        int r;
×
83

84
        assert(dest);
×
85
        assert(template);
×
86
        assert(sshd_binary);
×
87
        assert(generated_sshd_template_unit);
×
88

89
        /* If the system has a suitable template already, symlink it to the name we want to reuse it */
90
        if (found_sshd_template_service)
×
91
                return generator_add_symlink(
×
92
                                dest,
93
                                template,
94
                                /* dep_type= */ NULL,
95
                                found_sshd_template_service);
96

97
        if (!*generated_sshd_template_unit) {
×
98
                _cleanup_fclose_ FILE *f = NULL;
×
99

100
                r = generator_open_unit_file_full(
×
101
                                dest,
102
                                /* source= */ NULL,
103
                                "sshd-generated@.service", /* Give this generated unit a generic name, since we want to use it for both AF_UNIX and AF_VSOCK */
104
                                &f,
105
                                generated_sshd_template_unit,
106
                                /* ret_temp_path= */ NULL);
107
                if (r < 0)
×
108
                        return r;
109

110
                fprintf(f,
×
111
                        "[Unit]\n"
112
                        "Description=OpenSSH Per-Connection Server Daemon\n"
113
                        "Documentation=man:systemd-ssh-generator(8) man:sshd(8)\n"
114
                        "\n"
115
                        "[Service]\n"
116
                        "ExecStart=-%s -i -o \"AuthorizedKeysFile ${CREDENTIALS_DIRECTORY}/ssh.ephemeral-authorized_keys-all .ssh/authorized_keys\"\n"
117
                        "StandardInput=socket\n"
118
                        "ImportCredential=ssh.ephemeral-authorized_keys-all\n",
119
                        sshd_binary);
120

121
                r = fflush_and_check(f);
×
122
                if (r < 0)
×
123
                        return log_error_errno(r, "Failed to write sshd template: %m");
×
124
        }
125

126
        return generator_add_symlink(
×
127
                        dest,
128
                        template,
129
                        /* dep_type= */ NULL,
130
                        *generated_sshd_template_unit);
131
}
132

133
static int write_socket_unit(
×
134
                const char *dest,
135
                const char *unit,
136
                const char *listen_stream,
137
                const char *comment,
138
                bool with_ssh_access_target_dependency) {
139

140
        int r;
×
141

142
        assert(dest);
×
143
        assert(unit);
×
144
        assert(listen_stream);
×
145
        assert(comment);
×
146

147
        _cleanup_fclose_ FILE *f = NULL;
×
148
        r = generator_open_unit_file(
×
149
                        dest,
150
                        /* source= */ NULL,
151
                        unit,
152
                        &f);
153
        if (r < 0)
×
154
                return r;
155

156
        fprintf(f,
×
157
                "[Unit]\n"
158
                "Description=OpenSSH Server Socket (systemd-ssh-generator, %s)\n"
159
                "Documentation=man:systemd-ssh-generator(8)\n",
160
                comment);
161

162
        /* When this is a remotely accessible socket let's mark this with a milestone: ssh-access.target */
163
        if (with_ssh_access_target_dependency)
×
164
                fputs("Wants=ssh-access.target\n"
×
165
                      "Before=ssh-access.target\n",
166
                      f);
167

168
        fprintf(f,
×
169
                "\n[Socket]\n"
170
                "ListenStream=%s\n"
171
                "Accept=yes\n"
172
                "PollLimitIntervalSec=30s\n"
173
                "PollLimitBurst=50\n",
174
                listen_stream);
175

176
        r = fflush_and_check(f);
×
177
        if (r < 0)
×
178
                return log_error_errno(r, "Failed to write %s SSH socket unit: %m", comment);
×
179

180
        r = generator_add_symlink(
×
181
                        dest,
182
                        SPECIAL_SOCKETS_TARGET,
183
                        "wants",
184
                        unit);
185
        if (r < 0)
×
186
                return r;
×
187

188
        return 0;
189
}
190

191
static int add_vsock_socket(
×
192
                const char *dest,
193
                const char *sshd_binary,
194
                const char *found_sshd_template_unit,
195
                char **generated_sshd_template_unit) {
196

197
        int r;
×
198

199
        assert(dest);
×
200
        assert(generated_sshd_template_unit);
×
201

202
        Virtualization v = detect_virtualization();
×
203
        if (v < 0)
×
204
                return log_error_errno(v, "Failed to detect if we run in a VM: %m");
×
205
        if (!VIRTUALIZATION_IS_VM(v)) {
×
206
                /* NB: if we are running in a container inside a VM, then we'll *not* do AF_VSOCK stuff */
207
                log_debug("Not running in a VM, not listening on AF_VSOCK.");
×
208
                return 0;
×
209
        }
210

211
        _cleanup_close_ int vsock_fd = socket(AF_VSOCK, SOCK_STREAM|SOCK_CLOEXEC, 0);
×
212
        if (vsock_fd < 0) {
×
213
                if (ERRNO_IS_NOT_SUPPORTED(errno)) {
×
214
                        log_debug("Not creating AF_VSOCK ssh listener, since AF_VSOCK is not available.");
×
215
                        return 0;
×
216
                }
217

218
                return log_error_errno(errno, "Unable to test if AF_VSOCK is available: %m");
×
219
        }
220

221
        vsock_fd = safe_close(vsock_fd);
×
222

223
        /* Determine the local CID so that we can log it to help users to connect to this VM */
224
        unsigned local_cid;
×
225
        r = vsock_get_local_cid(&local_cid);
×
226
        if (r < 0) {
×
227
                if (ERRNO_IS_DEVICE_ABSENT(r)) {
×
228
                        log_debug("Not creating AF_VSOCK ssh listener, since /dev/vsock is not available (even though AF_VSOCK is).");
×
229
                        return 0;
×
230
                }
231

232
                return log_error_errno(r, "Failed to query local AF_VSOCK CID: %m");
×
233
        }
234

235
        r = make_sshd_template_unit(
×
236
                        dest,
237
                        "sshd-vsock@.service",
238
                        sshd_binary,
239
                        found_sshd_template_unit,
240
                        generated_sshd_template_unit);
241
        if (r < 0)
×
242
                return r;
243

244
        r = write_socket_unit(
×
245
                        dest,
246
                        "sshd-vsock.socket",
247
                        "vsock::22",
248
                        "AF_VSOCK",
249
                        /* with_ssh_access_target_dependency= */ true);
250
        if (r < 0)
×
251
                return r;
252

253
        log_debug("Binding SSH to AF_VSOCK vsock::22.\n"
×
254
                  "→ connect via 'ssh vsock/%u' from host", local_cid);
255
        return 0;
256
}
257

258
static int add_local_unix_socket(
×
259
                const char *dest,
260
                const char *sshd_binary,
261
                const char *found_sshd_template_unit,
262
                char **generated_sshd_template_unit) {
263

264
        int r;
×
265

266
        assert(dest);
×
267
        assert(sshd_binary);
×
268
        assert(generated_sshd_template_unit);
×
269

270
        r = make_sshd_template_unit(
×
271
                        dest,
272
                        "sshd-unix-local@.service",
273
                        sshd_binary,
274
                        found_sshd_template_unit,
275
                        generated_sshd_template_unit);
276
        if (r < 0)
×
277
                return r;
278

279
        r = write_socket_unit(
×
280
                        dest,
281
                        "sshd-unix-local.socket",
282
                        "/run/ssh-unix-local/socket",
283
                        "AF_UNIX Local",
284
                        /* with_ssh_access_target_dependency= */ false);
285
        if (r < 0)
×
286
                return r;
287

288
        log_debug("Binding SSH to AF_UNIX socket /run/ssh-unix-local/socket.\n"
×
289
                  "→ connect via 'ssh .host' locally");
290
        return 0;
291
}
292

293
static int add_export_unix_socket(
×
294
                const char *dest,
295
                const char *sshd_binary,
296
                const char *found_sshd_template_unit,
297
                char **generated_sshd_template_unit) {
298

299
        int r;
×
300

301
        assert(dest);
×
302
        assert(sshd_binary);
×
303
        assert(generated_sshd_template_unit);
×
304

305
        Virtualization v = detect_container();
×
306
        if (v < 0)
×
307
                return log_error_errno(v, "Failed to detect if we run in a container: %m");
×
308
        if (v == VIRTUALIZATION_NONE) {
×
309
                log_debug("Not running in container, not listening on /run/host/unix-export/ssh");
×
310
                return 0;
×
311
        }
312

313
        if (access("/run/host/unix-export/", W_OK) < 0) {
×
314
                if (errno == ENOENT) {
×
315
                        log_debug("Container manager does not provide /run/host/unix-export/ mount, not binding AF_UNIX socket there.");
×
316
                        return 0;
×
317
                }
318
                if (errno == EROFS || ERRNO_IS_PRIVILEGE(errno)) {
×
319
                        log_debug("Container manager does not provide write access to /run/host/unix-export/, not binding AF_UNIX socket there.");
×
320
                        return 0;
×
321
                }
322

323
                return log_error_errno(errno, "Unable to check if /run/host/unix-export exists: %m");
×
324
        }
325

326
        r = make_sshd_template_unit(
×
327
                        dest,
328
                        "sshd-unix-export@.service",
329
                        sshd_binary,
330
                        found_sshd_template_unit,
331
                        generated_sshd_template_unit);
332
        if (r < 0)
×
333
                return r;
334

335
        r = write_socket_unit(
×
336
                        dest,
337
                        "sshd-unix-export.socket",
338
                        "/run/host/unix-export/ssh",
339
                        "AF_UNIX Export",
340
                        /* with_ssh_access_target_dependency= */ true);
341
        if (r < 0)
×
342
                return r;
343

344
        log_debug("Binding SSH to AF_UNIX socket /run/host/unix-export/ssh\n"
×
345
                  "→ connect via 'ssh unix/run/systemd/nspawn/unix-export/\?\?\?/ssh' from host");
346

347
        return 0;
348
}
349

350
static int add_extra_sockets(
×
351
                const char *dest,
352
                const char *sshd_binary,
353
                const char *found_sshd_template_unit,
354
                char **generated_sshd_template_unit) {
355

356
        unsigned n = 1;
×
357
        int r;
×
358

359
        assert(dest);
×
360
        assert(sshd_binary);
×
361
        assert(generated_sshd_template_unit);
×
362

363
        if (strv_isempty(arg_listen_extra))
×
364
                return 0;
365

366
        STRV_FOREACH(i, arg_listen_extra) {
×
367
                _cleanup_free_ char *service = NULL, *socket = NULL;
×
368

369
                if (n > 1) {
×
370
                        if (asprintf(&service, "sshd-extra-%u@.service", n) < 0)
×
371
                                return log_oom();
×
372

373
                        if (asprintf(&socket, "sshd-extra-%u.socket", n) < 0)
×
374
                                return log_oom();
×
375
                }
376

377
                r = make_sshd_template_unit(
×
378
                                dest,
379
                                service ?: "sshd-extra@.service",
×
380
                                sshd_binary,
381
                                found_sshd_template_unit,
382
                                generated_sshd_template_unit);
383
                if (r < 0)
×
384
                        return r;
385

386
                r = write_socket_unit(
×
387
                                dest,
388
                                socket ?: "sshd-extra.socket",
×
389
                                *i,
390
                                *i,
391
                                /* with_ssh_access_target_dependency= */ true);
392
                if (r < 0)
×
393
                        return r;
394

395
                log_debug("Binding SSH to socket %s.", *i);
×
396
                n++;
×
397
        }
398

399
        return 0;
400
}
401

402
static int parse_credentials(void) {
×
403
        _cleanup_free_ char *b = NULL;
×
404
        size_t sz = 0;
×
405
        int r;
×
406

407
        r = read_credential_with_decryption("ssh.listen", (void**) &b, &sz);
×
408
        if (r <= 0)
×
409
                return r;
410

411
        _cleanup_fclose_ FILE *f = NULL;
×
412
        f = fmemopen_unlocked(b, sz, "r");
×
413
        if (!f)
×
414
                return log_oom();
×
415

416
        for (;;) {
×
417
                _cleanup_free_ char *item = NULL;
×
418

419
                r = read_stripped_line(f, LINE_MAX, &item);
×
420
                if (r == 0)
×
421
                        break;
422
                if (r < 0) {
×
423
                        log_error_errno(r, "Failed to parse credential 'ssh.listen': %m");
×
424
                        break;
425
                }
426

427
                if (startswith(item, "#"))
×
428
                        continue;
×
429

430
                SocketAddress sa;
×
431
                r = socket_address_parse(&sa, item);
×
432
                if (r < 0) {
×
433
                        log_warning_errno(r, "Failed to parse systemd.ssh_listen= expression, ignoring: %s", item);
×
434
                        continue;
×
435
                }
436

437
                _cleanup_free_ char *s = NULL;
×
438
                r = socket_address_print(&sa, &s);
×
439
                if (r < 0)
×
440
                        return log_error_errno(r, "Failed to format socket address: %m");
×
441

442
                if (strv_consume(&arg_listen_extra, TAKE_PTR(s)) < 0)
×
443
                        return log_oom();
×
444
        }
445

446
        return 0;
×
447
}
448

449
static int run(const char *dest, const char *dest_early, const char *dest_late) {
×
450
        int r;
×
451

452
        assert_se(arg_dest = dest);
×
453

454
        r = proc_cmdline_parse(parse_proc_cmdline_item, /* userdata= */ NULL, /* flags= */ 0);
×
455
        if (r < 0)
×
456
                log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");
×
457

458
        (void) parse_credentials();
×
459

460
        strv_sort_uniq(arg_listen_extra);
×
461

462
        if (!arg_auto && strv_isempty(arg_listen_extra)) {
×
463
                log_debug("Disabling SSH generator logic, because it has been turned off explicitly.");
×
464
                return 0;
×
465
        }
466

467
        _cleanup_free_ char *sshd_binary = NULL;
×
468
        r = find_executable("sshd", &sshd_binary);
×
469
        if (r == -ENOENT) {
×
470
                log_debug("Disabling SSH generator logic, since sshd is not installed.");
×
471
                return 0;
×
472
        }
473
        if (r < 0)
×
474
                return log_error_errno(r, "Failed to determine if sshd is installed: %m");
×
475

476
        _cleanup_(lookup_paths_done) LookupPaths lp = {};
×
477
        r = lookup_paths_init_or_warn(&lp, RUNTIME_SCOPE_SYSTEM, LOOKUP_PATHS_EXCLUDE_GENERATED, /* root_dir= */ NULL);
×
478
        if (r < 0)
×
479
                return r;
480

481
        _cleanup_free_ char *found_sshd_template_unit = NULL;
×
482
        r = unit_file_exists_full(RUNTIME_SCOPE_SYSTEM, &lp, "sshd@.service", &found_sshd_template_unit);
×
483
        if (r < 0)
×
484
                return log_error_errno(r, "Unable to detect if sshd@.service exists: %m");
×
485

486
        _cleanup_free_ char *generated_sshd_template_unit = NULL;
×
487
        RET_GATHER(r, add_extra_sockets(dest, sshd_binary, found_sshd_template_unit, &generated_sshd_template_unit));
×
488

489
        if (arg_auto) {
×
490
                RET_GATHER(r, add_vsock_socket(dest, sshd_binary, found_sshd_template_unit, &generated_sshd_template_unit));
×
491
                RET_GATHER(r, add_local_unix_socket(dest, sshd_binary, found_sshd_template_unit, &generated_sshd_template_unit));
×
492
                RET_GATHER(r, add_export_unix_socket(dest, sshd_binary, found_sshd_template_unit, &generated_sshd_template_unit));
×
493
        }
494

495
        return r;
×
496
}
497

498
DEFINE_MAIN_GENERATOR_FUNCTION(run);
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc