]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/escape/escape.c
676b7dce54bcd8fa7bf2f0036e1cdafaab9d2151
[thirdparty/systemd.git] / src / escape / escape.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <getopt.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6
7 #include "alloc-util.h"
8 #include "log.h"
9 #include "main-func.h"
10 #include "path-util.h"
11 #include "pretty-print.h"
12 #include "string-util.h"
13 #include "strv.h"
14 #include "unit-name.h"
15
16 static enum {
17 ACTION_ESCAPE,
18 ACTION_UNESCAPE,
19 ACTION_MANGLE
20 } arg_action = ACTION_ESCAPE;
21 static const char *arg_suffix = NULL;
22 static const char *arg_template = NULL;
23 static bool arg_path = false;
24 static bool arg_instance = false;
25
26 static int help(void) {
27 _cleanup_free_ char *link = NULL;
28 int r;
29
30 r = terminal_urlify_man("systemd-escape", "1", &link);
31 if (r < 0)
32 return log_oom();
33
34 printf("%s [OPTIONS...] [NAME...]\n\n"
35 "Escape strings for usage in systemd unit names.\n\n"
36 " -h --help Show this help\n"
37 " --version Show package version\n"
38 " --suffix=SUFFIX Unit suffix to append to escaped strings\n"
39 " --template=TEMPLATE Insert strings as instance into template\n"
40 " --instance With --unescape, show just the instance part\n"
41 " -u --unescape Unescape strings\n"
42 " -m --mangle Mangle strings\n"
43 " -p --path When escaping/unescaping assume the string is a path\n"
44 "\nSee the %s for details.\n",
45 program_invocation_short_name,
46 link);
47
48 return 0;
49 }
50
51 static int parse_argv(int argc, char *argv[]) {
52
53 enum {
54 ARG_VERSION = 0x100,
55 ARG_SUFFIX,
56 ARG_TEMPLATE
57 };
58
59 static const struct option options[] = {
60 { "help", no_argument, NULL, 'h' },
61 { "version", no_argument, NULL, ARG_VERSION },
62 { "suffix", required_argument, NULL, ARG_SUFFIX },
63 { "template", required_argument, NULL, ARG_TEMPLATE },
64 { "unescape", no_argument, NULL, 'u' },
65 { "mangle", no_argument, NULL, 'm' },
66 { "path", no_argument, NULL, 'p' },
67 { "instance", no_argument, NULL, 'i' },
68 {}
69 };
70
71 int c;
72
73 assert(argc >= 0);
74 assert(argv);
75
76 while ((c = getopt_long(argc, argv, "hump", options, NULL)) >= 0)
77
78 switch (c) {
79
80 case 'h':
81 return help();
82
83 case ARG_VERSION:
84 return version();
85
86 case ARG_SUFFIX: {
87 UnitType t = unit_type_from_string(optarg);
88 if (t < 0)
89 return log_error_errno(t, "Invalid unit suffix type \"%s\".", optarg);
90
91 arg_suffix = optarg;
92 break;
93 }
94
95 case ARG_TEMPLATE:
96 if (!unit_name_is_valid(optarg, UNIT_NAME_TEMPLATE))
97 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
98 "Template name %s is not valid.", optarg);
99
100 arg_template = optarg;
101 break;
102
103 case 'u':
104 arg_action = ACTION_UNESCAPE;
105 break;
106
107 case 'm':
108 arg_action = ACTION_MANGLE;
109 break;
110
111 case 'p':
112 arg_path = true;
113 break;
114
115 case 'i':
116 arg_instance = true;
117 break;
118
119 case '?':
120 return -EINVAL;
121
122 default:
123 assert_not_reached();
124 }
125
126 if (optind >= argc)
127 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
128 "Not enough arguments.");
129
130 if (arg_template && arg_suffix)
131 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
132 "--suffix= and --template= may not be combined.");
133
134 if ((arg_template || arg_suffix) && arg_action == ACTION_MANGLE)
135 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
136 "--suffix= and --template= are not compatible with --mangle.");
137
138 if (arg_suffix && arg_action == ACTION_UNESCAPE)
139 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
140 "--suffix is not compatible with --unescape.");
141
142 if (arg_path && !IN_SET(arg_action, ACTION_ESCAPE, ACTION_UNESCAPE))
143 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
144 "--path may not be combined with --mangle.");
145
146 if (arg_instance && arg_action != ACTION_UNESCAPE)
147 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
148 "--instance must be used in conjunction with --unescape.");
149
150 if (arg_instance && arg_template)
151 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
152 "--instance may not be combined with --template.");
153
154 return 1;
155 }
156
157 static int run(int argc, char *argv[]) {
158 char **i;
159 int r;
160
161 log_setup();
162
163 r = parse_argv(argc, argv);
164 if (r <= 0)
165 return r;
166
167 STRV_FOREACH(i, argv + optind) {
168 _cleanup_free_ char *e = NULL;
169
170 switch (arg_action) {
171
172 case ACTION_ESCAPE:
173 if (arg_path) {
174 r = unit_name_path_escape(*i, &e);
175 if (r < 0) {
176 if (r == -EINVAL) {
177 /* If escaping failed because the string was invalid, let's print a
178 * friendly message about it. Catch these specific error cases
179 * explicitly. */
180
181 if (!path_is_valid(*i))
182 return log_error_errno(r, "Input '%s' is not a valid file system path, failed to escape.", *i);
183 if (!path_is_absolute(*i))
184 return log_error_errno(r, "Input '%s' is not an absolute file system path, failed to escape.", *i);
185 if (!path_is_normalized(*i))
186 return log_error_errno(r, "Input '%s' is not a normalized file system path, failed to escape.", *i);
187 }
188
189 /* All other error cases. */
190 return log_error_errno(r, "Failed to escape string: %m");
191 }
192
193 /* If the escaping worked, then still warn if the path is not like we'd like
194 * it. Because that means escaping is not necessarily reversible. */
195
196 if (!path_is_valid(*i))
197 log_warning("Input '%s' is not a valid file system path, escaping is likely not going be reversible.", *i);
198 else if (!path_is_absolute(*i))
199 log_warning("Input '%s' is not an absolute file system path, escaping is likely not going to be reversible.", *i);
200
201 /* Note that we don't complain about paths not being normalized here, because
202 * some forms of non-normalization is actually OK, such as a series // and
203 * unit_name_path_escape() will clean those up silently, and the reversal is
204 * "close enough" to be OK. */
205 } else {
206 e = unit_name_escape(*i);
207 if (!e)
208 return log_oom();
209 }
210
211 if (arg_template) {
212 char *x;
213
214 r = unit_name_replace_instance(arg_template, e, &x);
215 if (r < 0)
216 return log_error_errno(r, "Failed to replace instance: %m");
217
218 free_and_replace(e, x);
219 } else if (arg_suffix) {
220 if (!strextend(&e, ".", arg_suffix))
221 return log_oom();
222 }
223
224 break;
225
226 case ACTION_UNESCAPE: {
227 _cleanup_free_ char *name = NULL;
228
229 if (arg_template || arg_instance) {
230 _cleanup_free_ char *template = NULL;
231
232 r = unit_name_to_instance(*i, &name);
233 if (r < 0)
234 return log_error_errno(r, "Failed to extract instance: %m");
235 if (isempty(name))
236 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
237 "Unit %s is missing the instance name.", *i);
238
239 r = unit_name_template(*i, &template);
240 if (r < 0)
241 return log_error_errno(r, "Failed to extract template: %m");
242 if (arg_template && !streq(arg_template, template))
243 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
244 "Unit %s template %s does not match specified template %s.",
245 *i, template, arg_template);
246 } else {
247 name = strdup(*i);
248 if (!name)
249 return log_oom();
250 }
251
252 if (arg_path)
253 r = unit_name_path_unescape(name, &e);
254 else
255 r = unit_name_unescape(name, &e);
256 if (r < 0)
257 return log_error_errno(r, "Failed to unescape string: %m");
258
259 break;
260 }
261
262 case ACTION_MANGLE:
263 r = unit_name_mangle(*i, 0, &e);
264 if (r < 0)
265 return log_error_errno(r, "Failed to mangle name: %m");
266
267 break;
268 }
269
270 if (i != argv + optind)
271 fputc(' ', stdout);
272
273 fputs(e, stdout);
274 }
275
276 fputc('\n', stdout);
277
278 return 0;
279 }
280
281 DEFINE_MAIN_FUNCTION(run);