Debian Codegraph @

src/ssh-generator/ssh-issue.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <sys/stat.h>
4 #include <unistd.h>
5
6 #include "alloc-util.h"
7 #include "ansi-color.h"
8 #include "build.h"
9 #include "fd-util.h"
10 #include "format-table.h"
11 #include "fs-util.h"
12 #include "log.h"
13 #include "main-func.h"
14 #include "mkdir.h"
15 #include "options.h"
16 #include "parse-argument.h"
17 #include "pretty-print.h"
18 #include "ssh-util.h"
19 #include "string-util.h"
20 #include "strv.h"
21 #include "tmpfile-util.h"
22 #include "verbs.h"
23 #include "virt.h"
24
25 static char *arg_issue_path = NULL;
26 static bool arg_issue_stdout = false;
27
28 STATIC_DESTRUCTOR_REGISTER(arg_issue_path, freep);
29
30 static int acquire_cid(unsigned *ret_cid) {
31 int r;
32
33 assert(ret_cid);
34
35 Virtualization v = detect_virtualization();
36 if (v < 0)
37 return log_error_errno(v, "Failed to detect if we run in a VM: %m");
38 if (!VIRTUALIZATION_IS_VM(v)) {
39 /* NB: if we are running in a container inside a VM, then we'll *not* do AF_VSOCK stuff */
40 log_debug("Not running in a VM, not creating issue file.");
41 *ret_cid = 0;
42 return 0;
43 }
44
45 r = vsock_open_or_warn(/* ret= */ NULL);
46 if (r <= 0)
47 return r;
48
49 return vsock_get_local_cid_or_warn(ret_cid);
50 }
51
52 VERB_NOARG(verb_make_vsock, "make-vsock",
53 "Generate the issue file");
54 static int verb_make_vsock(int argc, char *argv[], uintptr_t _data, void *_userdata) {
55 unsigned cid;
56 int r;
57
58 r = acquire_cid(&cid);
59 if (r < 0)
60 return r;
61 if (r == 0) {
62 log_debug("Not running in a VSOCK enabled VM, skipping.");
63 return 0;
64 }
65
66 _cleanup_(unlink_and_freep) char *t = NULL;
67 _cleanup_(fclosep) FILE *f = NULL;
68 FILE *out;
69
70 if (arg_issue_path) {
71 r = mkdir_parents(arg_issue_path, 0755);
72 if (r < 0)
73 return log_error_errno(r, "Failed to create parent directories of '%s': %m", arg_issue_path);
74
75 r = fopen_tmpfile_linkable(arg_issue_path, O_WRONLY|O_CLOEXEC, &t, &f);
76 if (r < 0)
77 return log_error_errno(r, "Failed to create '%s': %m", arg_issue_path);
78
79 out = f;
80 } else
81 out = stdout;
82
83 fprintf(out,
84 "Try contacting this VM's SSH server via 'ssh vsock%%%u' from host.\n"
85 "\n", cid);
86
87 if (f) {
88 if (fchmod(fileno(f), 0644) < 0)
89 return log_error_errno(errno, "Failed to adjust access mode of '%s': %m", arg_issue_path);
90
91 r = flink_tmpfile(f, t, arg_issue_path, LINK_TMPFILE_REPLACE);
92 if (r < 0)
93 return log_error_errno(r, "Failed to move '%s' into place: %m", arg_issue_path);
94 }
95
96 return 0;
97 }
98
99 VERB_NOARG(verb_rm_vsock, "rm-vsock",
100 "Remove the issue file");
101 static int verb_rm_vsock(int argc, char *argv[], uintptr_t _data, void *_userdata) {
102 if (arg_issue_path) {
103 if (unlink(arg_issue_path) < 0) {
104 if (errno != ENOENT)
105 return log_error_errno(errno, "Failed to remove '%s': %m", arg_issue_path);
106
107 log_debug_errno(errno, "File '%s' does not exist, no operation executed.", arg_issue_path);
108 } else
109 log_debug("Successfully removed '%s'.", arg_issue_path);
110 } else
111 log_notice("STDOUT selected for issue file, not removing.");
112
113 return 0;
114 }
115
116 static int help(void) {
117 _cleanup_free_ char *link = NULL;
118 _cleanup_(table_unrefp) Table *options = NULL, *verbs = NULL;
119 int r;
120
121 r = terminal_urlify_man("systemd-ssh-issue", "1", &link);
122 if (r < 0)
123 return log_oom();
124
125 r = verbs_get_help_table(&verbs);
126 if (r < 0)
127 return r;
128
129 r = option_parser_get_help_table(&options);
130 if (r < 0)
131 return r;
132
133 (void) table_sync_column_widths(0, verbs, options);
134
135 printf("%s [OPTIONS...] COMMAND\n"
136 "\n%sCreate/remove ssh /run/issue.d/ file reporting VSOCK address.%s\n"
137 "\n%sCommands:%s\n",
138 program_invocation_short_name,
139 ansi_highlight(), ansi_normal(),
140 ansi_underline(), ansi_normal());
141
142 r = table_print_or_warn(verbs);
143 if (r < 0)
144 return r;
145
146 printf("\n%sOptions:%s\n",
147 ansi_underline(), ansi_normal());
148
149 r = table_print_or_warn(options);
150 if (r < 0)
151 return r;
152
153 printf("\nSee the %s for details.\n", link);
154 return 0;
155 }
156
157 static int parse_argv(int argc, char *argv[], char ***ret_args) {
158 assert(argc >= 0);
159 assert(argv);
160 assert(ret_args);
161
162 OptionParser opts = { argc, argv };
163 const char *verb = NULL;
164 int r;
165
166 FOREACH_OPTION_OR_RETURN(c, &opts)
167 switch (c) {
168
169 OPTION_COMMON_HELP:
170 return help();
171
172 OPTION_COMMON_VERSION:
173 return version();
174
175 OPTION_LONG("make-vsock", NULL, /* help= */ NULL): {}
176 OPTION_LONG("rm-vsock", NULL, /* help= */ NULL):
177 verb = opts.opt->long_code;
178 break;
179
180 OPTION_LONG("issue-path", "PATH",
181 "Change path to /run/issue.d/50-ssh-vsock.issue"):
182 if (empty_or_dash(opts.arg)) {
183 arg_issue_path = mfree(arg_issue_path);
184 arg_issue_stdout = true;
185 break;
186 }
187
188 r = parse_path_argument(opts.arg, /* suppress_root= */ false, &arg_issue_path);
189 if (r < 0)
190 return r;
191
192 arg_issue_stdout = false;
193 break;
194 }
195
196 if (!arg_issue_path && !arg_issue_stdout) {
197 arg_issue_path = strdup("/run/issue.d/50-ssh-vsock.issue");
198 if (!arg_issue_path)
199 return log_oom();
200 }
201
202 char **args;
203 if (verb) {
204 if (option_parser_get_n_args(&opts) > 0)
205 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid use of compat option --make-vsock/--rm-vsock.");
206 log_warning("Options --make-vsock/--rm-vsock have been replaced by make-vsock/rm-vsock verbs.");
207 args = strv_new(verb);
208 } else
209 args = strv_copy(option_parser_get_args(&opts));
210 if (!args)
211 return log_oom();
212
213 *ret_args = args;
214 return 1;
215 }
216
217 static int run(int argc, char* argv[]) {
218 _cleanup_strv_free_ char **args = NULL;
219 int r;
220
221 log_setup();
222
223 r = parse_argv(argc, argv, &args);
224 if (r <= 0)
225 return r;
226
227 return dispatch_verb(args, NULL);
228 }
229
230 DEFINE_MAIN_FUNCTION(run);