1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
10 #include "alloc-util.h"
11 #include "glob-util.h"
12 #include "hexdecoct.h"
13 #include "memory-util.h"
14 #include "path-util.h"
15 #include "random-util.h"
16 #include "sparse-endian.h"
18 #include "stdio-util.h"
19 #include "string-util.h"
21 #include "unit-name.h"
23 /* Characters valid in a unit name. */
29 /* The same, but also permits the single @ character that may appear */
30 #define VALID_CHARS_WITH_AT \
34 /* All chars valid in a unit name glob */
35 #define VALID_CHARS_GLOB \
39 #define LONG_UNIT_NAME_HASH_KEY SD_ID128_MAKE(ec,f2,37,fb,58,32,4a,32,84,9f,06,9b,0d,21,eb,9a)
40 #define UNIT_NAME_HASH_LENGTH_CHARS 16
42 bool unit_name_is_valid(const char *n
, UnitNameFlags flags
) {
43 const char *e
, *i
, *at
;
45 assert((flags
& ~(UNIT_NAME_PLAIN
|UNIT_NAME_INSTANCE
|UNIT_NAME_TEMPLATE
)) == 0);
47 if (_unlikely_(flags
== 0))
53 if (strlen(n
) >= UNIT_NAME_MAX
)
60 if (unit_type_from_string(e
+ 1) < 0)
63 for (i
= n
, at
= NULL
; i
< e
; i
++) {
68 if (!strchr(VALID_CHARS_WITH_AT
, *i
))
75 if (flags
& UNIT_NAME_PLAIN
)
79 if (flags
& UNIT_NAME_INSTANCE
)
83 if (flags
& UNIT_NAME_TEMPLATE
)
84 if (at
&& e
== at
+ 1)
90 bool unit_prefix_is_valid(const char *p
) {
92 /* We don't allow additional @ in the prefix string */
97 return in_charset(p
, VALID_CHARS
);
100 bool unit_instance_is_valid(const char *i
) {
102 /* The max length depends on the length of the string, so we
103 * don't really check this here. */
108 /* We allow additional @ in the instance string, we do not
109 * allow them in the prefix! */
111 return in_charset(i
, "@" VALID_CHARS
);
114 bool unit_suffix_is_valid(const char *s
) {
121 if (unit_type_from_string(s
+ 1) < 0)
127 int unit_name_to_prefix(const char *n
, char **ret
) {
134 if (!unit_name_is_valid(n
, UNIT_NAME_ANY
))
143 s
= strndup(n
, p
- n
);
151 UnitNameFlags
unit_name_to_instance(const char *n
, char **ret
) {
156 if (!unit_name_is_valid(n
, UNIT_NAME_ANY
))
159 /* Everything past the first @ and before the last . is the instance */
164 return UNIT_NAME_PLAIN
;
174 char *i
= strndup(p
, d
-p
);
180 return d
> p
? UNIT_NAME_INSTANCE
: UNIT_NAME_TEMPLATE
;
183 int unit_name_to_prefix_and_instance(const char *n
, char **ret
) {
190 if (!unit_name_is_valid(n
, UNIT_NAME_ANY
))
197 s
= strndup(n
, d
- n
);
205 UnitType
unit_name_to_type(const char *n
) {
210 if (!unit_name_is_valid(n
, UNIT_NAME_ANY
))
211 return _UNIT_TYPE_INVALID
;
213 assert_se(e
= strrchr(n
, '.'));
215 return unit_type_from_string(e
+ 1);
218 int unit_name_change_suffix(const char *n
, const char *suffix
, char **ret
) {
219 _cleanup_free_
char *s
= NULL
;
227 if (!unit_name_is_valid(n
, UNIT_NAME_ANY
))
230 if (!unit_suffix_is_valid(suffix
))
233 assert_se(e
= strrchr(n
, '.'));
238 s
= new(char, a
+ b
+ 1);
242 strcpy(mempcpy(s
, n
, a
), suffix
);
244 /* Make sure the name is still valid (i.e. didn't grow too large due to longer suffix) */
245 if (!unit_name_is_valid(s
, UNIT_NAME_ANY
))
252 int unit_name_build(const char *prefix
, const char *instance
, const char *suffix
, char **ret
) {
259 if (suffix
[0] != '.')
262 type
= unit_type_from_string(suffix
+ 1);
266 return unit_name_build_from_type(prefix
, instance
, type
, ret
);
269 int unit_name_build_from_type(const char *prefix
, const char *instance
, UnitType type
, char **ret
) {
270 _cleanup_free_
char *s
= NULL
;
275 assert(type
< _UNIT_TYPE_MAX
);
278 if (!unit_prefix_is_valid(prefix
))
281 ut
= unit_type_to_string(type
);
284 if (!unit_instance_is_valid(instance
))
287 s
= strjoin(prefix
, "@", instance
, ".", ut
);
289 s
= strjoin(prefix
, ".", ut
);
293 /* Verify that this didn't grow too large (or otherwise is invalid) */
294 if (!unit_name_is_valid(s
, instance
? UNIT_NAME_INSTANCE
: UNIT_NAME_PLAIN
))
301 static char *do_escape_char(char c
, char *t
) {
306 *(t
++) = hexchar(c
>> 4);
312 static char *do_escape(const char *f
, char *t
) {
316 /* do not create units with a leading '.', like for "/.dotdir" mount points */
318 t
= do_escape_char(*f
, t
);
325 else if (IN_SET(*f
, '-', '\\') || !strchr(VALID_CHARS
, *f
))
326 t
= do_escape_char(*f
, t
);
334 char *unit_name_escape(const char *f
) {
339 r
= new(char, strlen(f
)*4+1);
349 int unit_name_unescape(const char *f
, char **ret
) {
350 _cleanup_free_
char *r
= NULL
;
359 for (t
= r
; *f
; f
++) {
362 else if (*f
== '\\') {
376 *(t
++) = (char) (((uint8_t) a
<< 4U) | (uint8_t) b
);
389 int unit_name_path_escape(const char *f
, char **ret
) {
390 _cleanup_free_
char *p
= NULL
;
402 if (empty_or_root(p
))
405 if (!path_is_normalized(p
))
408 /* Truncate trailing slashes and skip leading slashes */
409 delete_trailing_chars(p
, "/");
410 s
= unit_name_escape(skip_leading_chars(p
, "/"));
419 int unit_name_path_unescape(const char *f
, char **ret
) {
420 _cleanup_free_
char *s
= NULL
;
433 _cleanup_free_
char *w
= NULL
;
435 r
= unit_name_unescape(f
, &w
);
439 /* Don't accept trailing or leading slashes */
440 if (startswith(w
, "/") || endswith(w
, "/"))
443 /* Prefix a slash again */
448 if (!path_is_normalized(s
))
458 int unit_name_replace_instance(const char *f
, const char *i
, char **ret
) {
459 _cleanup_free_
char *s
= NULL
;
467 if (!unit_name_is_valid(f
, UNIT_NAME_INSTANCE
|UNIT_NAME_TEMPLATE
))
469 if (!unit_instance_is_valid(i
))
472 assert_se(p
= strchr(f
, '@'));
473 assert_se(e
= strrchr(f
, '.'));
478 s
= new(char, a
+ 1 + b
+ strlen(e
) + 1);
482 strcpy(mempcpy(mempcpy(s
, f
, a
+ 1), i
, b
), e
);
484 /* Make sure the resulting name still is valid, i.e. didn't grow too large */
485 if (!unit_name_is_valid(s
, UNIT_NAME_INSTANCE
))
492 int unit_name_template(const char *f
, char **ret
) {
500 if (!unit_name_is_valid(f
, UNIT_NAME_INSTANCE
|UNIT_NAME_TEMPLATE
))
503 assert_se(p
= strchr(f
, '@'));
504 assert_se(e
= strrchr(f
, '.'));
508 s
= new(char, a
+ 1 + strlen(e
) + 1);
512 strcpy(mempcpy(s
, f
, a
+ 1), e
);
518 bool unit_name_is_hashed(const char *name
) {
521 if (!unit_name_is_valid(name
, UNIT_NAME_PLAIN
))
524 assert_se(s
= strrchr(name
, '.'));
526 if (s
- name
< UNIT_NAME_HASH_LENGTH_CHARS
+ 1)
529 s
-= UNIT_NAME_HASH_LENGTH_CHARS
;
533 for (size_t i
= 0; i
< UNIT_NAME_HASH_LENGTH_CHARS
; i
++)
534 if (!strchr(LOWERCASE_HEXDIGITS
, s
[i
]))
540 int unit_name_hash_long(const char *name
, char **ret
) {
541 _cleanup_free_
char *n
= NULL
, *hash
= NULL
;
546 if (strlen(name
) < UNIT_NAME_MAX
)
549 suffix
= strrchr(name
, '.');
553 if (unit_type_from_string(suffix
+1) < 0)
556 h
= htole64(siphash24_string(name
, LONG_UNIT_NAME_HASH_KEY
.bytes
));
558 hash
= hexmem(&h
, sizeof(h
));
562 assert_se(strlen(hash
) == UNIT_NAME_HASH_LENGTH_CHARS
);
564 len
= UNIT_NAME_MAX
- 1 - strlen(suffix
+1) - UNIT_NAME_HASH_LENGTH_CHARS
- 2;
565 assert(len
> 0 && len
< UNIT_NAME_MAX
);
567 n
= strndup(name
, len
);
571 if (!strextend(&n
, "_", hash
, suffix
))
573 assert_se(unit_name_is_valid(n
, UNIT_NAME_PLAIN
));
580 int unit_name_from_path(const char *path
, const char *suffix
, char **ret
) {
581 _cleanup_free_
char *p
= NULL
, *s
= NULL
;
588 if (!unit_suffix_is_valid(suffix
))
591 r
= unit_name_path_escape(path
, &p
);
595 s
= strjoin(p
, suffix
);
599 if (strlen(s
) >= UNIT_NAME_MAX
) {
600 _cleanup_free_
char *n
= NULL
;
602 log_debug("Unit name \"%s\" too long, falling back to hashed unit name.", s
);
604 r
= unit_name_hash_long(s
, &n
);
608 free_and_replace(s
, n
);
611 /* Refuse if this for some other reason didn't result in a valid name */
612 if (!unit_name_is_valid(s
, UNIT_NAME_PLAIN
))
619 int unit_name_from_path_instance(const char *prefix
, const char *path
, const char *suffix
, char **ret
) {
620 _cleanup_free_
char *p
= NULL
, *s
= NULL
;
628 if (!unit_prefix_is_valid(prefix
))
631 if (!unit_suffix_is_valid(suffix
))
634 r
= unit_name_path_escape(path
, &p
);
638 s
= strjoin(prefix
, "@", p
, suffix
);
642 if (strlen(s
) >= UNIT_NAME_MAX
) /* Return a slightly more descriptive error for this specific condition */
643 return -ENAMETOOLONG
;
645 /* Refuse if this for some other reason didn't result in a valid name */
646 if (!unit_name_is_valid(s
, UNIT_NAME_INSTANCE
))
653 int unit_name_to_path(const char *name
, char **ret
) {
654 _cleanup_free_
char *prefix
= NULL
;
659 r
= unit_name_to_prefix(name
, &prefix
);
663 if (unit_name_is_hashed(name
))
664 return -ENAMETOOLONG
;
666 return unit_name_path_unescape(prefix
, ret
);
669 static bool do_escape_mangle(const char *f
, bool allow_globs
, char *t
) {
670 const char *valid_chars
;
671 bool mangled
= false;
676 /* We'll only escape the obvious characters here, to play safe.
678 * Returns true if any characters were mangled, false otherwise.
681 valid_chars
= allow_globs
? VALID_CHARS_GLOB
: VALID_CHARS_WITH_AT
;
687 } else if (!strchr(valid_chars
, *f
)) {
688 t
= do_escape_char(*f
, t
);
698 * Convert a string to a unit name. /dev/blah is converted to dev-blah.device,
699 * /blah/blah is converted to blah-blah.mount, anything else is left alone,
700 * except that @suffix is appended if a valid unit suffix is not present.
702 * If @allow_globs, globs characters are preserved. Otherwise, they are escaped.
704 int unit_name_mangle_with_suffix(const char *name
, const char *operation
, UnitNameMangle flags
, const char *suffix
, char **ret
) {
705 _cleanup_free_
char *s
= NULL
;
706 bool mangled
, suggest_escape
= true;
713 if (isempty(name
)) /* We cannot mangle empty unit names to become valid, sorry. */
716 if (!unit_suffix_is_valid(suffix
))
719 /* Already a fully valid unit name? If so, no mangling is necessary... */
720 if (unit_name_is_valid(name
, UNIT_NAME_ANY
))
723 /* Already a fully valid globbing expression? If so, no mangling is necessary either... */
724 if (string_is_glob(name
) && in_charset(name
, VALID_CHARS_GLOB
)) {
725 if (flags
& UNIT_NAME_MANGLE_GLOB
)
727 log_full(flags
& UNIT_NAME_MANGLE_WARN
? LOG_NOTICE
: LOG_DEBUG
,
728 "Glob pattern passed%s%s, but globs are not supported for this.",
729 operation
? " " : "", strempty(operation
));
730 suggest_escape
= false;
733 if (is_device_path(name
)) {
734 r
= unit_name_from_path(name
, ".device", ret
);
741 if (path_is_absolute(name
)) {
742 r
= unit_name_from_path(name
, ".mount", ret
);
749 s
= new(char, strlen(name
) * 4 + strlen(suffix
) + 1);
753 mangled
= do_escape_mangle(name
, flags
& UNIT_NAME_MANGLE_GLOB
, s
);
755 log_full(flags
& UNIT_NAME_MANGLE_WARN
? LOG_NOTICE
: LOG_DEBUG
,
756 "Invalid unit name \"%s\" escaped as \"%s\"%s.",
758 suggest_escape
? " (maybe you should use systemd-escape?)" : "");
760 /* Append a suffix if it doesn't have any, but only if this is not a glob, so that we can allow
761 * "foo.*" as a valid glob. */
762 if ((!(flags
& UNIT_NAME_MANGLE_GLOB
) || !string_is_glob(s
)) && unit_name_to_type(s
) < 0)
765 /* Make sure mangling didn't grow this too large (but don't do this check if globbing is allowed,
766 * since globs generally do not qualify as valid unit names) */
767 if (!FLAGS_SET(flags
, UNIT_NAME_MANGLE_GLOB
) && !unit_name_is_valid(s
, UNIT_NAME_ANY
))
782 int slice_build_parent_slice(const char *slice
, char **ret
) {
783 _cleanup_free_
char *s
= NULL
;
790 if (!slice_name_is_valid(slice
))
793 if (streq(slice
, SPECIAL_ROOT_SLICE
)) {
802 dash
= strrchr(s
, '-');
804 strcpy(dash
, ".slice");
806 r
= free_and_strdup(&s
, SPECIAL_ROOT_SLICE
);
815 int slice_build_subslice(const char *slice
, const char *name
, char **ret
) {
822 if (!slice_name_is_valid(slice
))
825 if (!unit_prefix_is_valid(name
))
828 if (streq(slice
, SPECIAL_ROOT_SLICE
))
829 subslice
= strjoin(name
, ".slice");
833 assert_se(e
= endswith(slice
, ".slice"));
835 subslice
= new(char, (e
- slice
) + 1 + strlen(name
) + 6 + 1);
839 stpcpy(stpcpy(stpcpy(mempcpy(subslice
, slice
, e
- slice
), "-"), name
), ".slice");
846 bool slice_name_is_valid(const char *name
) {
850 if (!unit_name_is_valid(name
, UNIT_NAME_PLAIN
))
853 if (streq(name
, SPECIAL_ROOT_SLICE
))
856 e
= endswith(name
, ".slice");
860 for (p
= name
; p
< e
; p
++) {
864 /* Don't allow initial dash */
868 /* Don't allow multiple dashes */
877 /* Don't allow trailing hash */
884 bool unit_name_prefix_equal(const char *a
, const char *b
) {
890 if (!unit_name_is_valid(a
, UNIT_NAME_ANY
) || !unit_name_is_valid(b
, UNIT_NAME_ANY
))
904 return memcmp_nn(a
, p
- a
, b
, q
- b
) == 0;