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
;
397 r
= path_simplify_alloc(f
, &p
);
401 if (empty_or_root(p
))
404 if (!path_is_normalized(p
))
407 /* Truncate trailing slashes and skip leading slashes */
408 delete_trailing_chars(p
, "/");
409 s
= unit_name_escape(skip_leading_chars(p
, "/"));
418 int unit_name_path_unescape(const char *f
, char **ret
) {
419 _cleanup_free_
char *s
= NULL
;
432 _cleanup_free_
char *w
= NULL
;
434 r
= unit_name_unescape(f
, &w
);
438 /* Don't accept trailing or leading slashes */
439 if (startswith(w
, "/") || endswith(w
, "/"))
442 /* Prefix a slash again */
447 if (!path_is_normalized(s
))
457 int unit_name_replace_instance(const char *f
, const char *i
, char **ret
) {
458 _cleanup_free_
char *s
= NULL
;
466 if (!unit_name_is_valid(f
, UNIT_NAME_INSTANCE
|UNIT_NAME_TEMPLATE
))
468 if (!unit_instance_is_valid(i
))
471 assert_se(p
= strchr(f
, '@'));
472 assert_se(e
= strrchr(f
, '.'));
477 s
= new(char, a
+ 1 + b
+ strlen(e
) + 1);
481 strcpy(mempcpy(mempcpy(s
, f
, a
+ 1), i
, b
), e
);
483 /* Make sure the resulting name still is valid, i.e. didn't grow too large */
484 if (!unit_name_is_valid(s
, UNIT_NAME_INSTANCE
))
491 int unit_name_template(const char *f
, char **ret
) {
499 if (!unit_name_is_valid(f
, UNIT_NAME_INSTANCE
|UNIT_NAME_TEMPLATE
))
502 assert_se(p
= strchr(f
, '@'));
503 assert_se(e
= strrchr(f
, '.'));
507 s
= new(char, a
+ 1 + strlen(e
) + 1);
511 strcpy(mempcpy(s
, f
, a
+ 1), e
);
517 bool unit_name_is_hashed(const char *name
) {
520 if (!unit_name_is_valid(name
, UNIT_NAME_PLAIN
))
523 assert_se(s
= strrchr(name
, '.'));
525 if (s
- name
< UNIT_NAME_HASH_LENGTH_CHARS
+ 1)
528 s
-= UNIT_NAME_HASH_LENGTH_CHARS
;
532 for (size_t i
= 0; i
< UNIT_NAME_HASH_LENGTH_CHARS
; i
++)
533 if (!strchr(LOWERCASE_HEXDIGITS
, s
[i
]))
539 int unit_name_hash_long(const char *name
, char **ret
) {
540 _cleanup_free_
char *n
= NULL
, *hash
= NULL
;
545 if (strlen(name
) < UNIT_NAME_MAX
)
548 suffix
= strrchr(name
, '.');
552 if (unit_type_from_string(suffix
+1) < 0)
555 h
= htole64(siphash24_string(name
, LONG_UNIT_NAME_HASH_KEY
.bytes
));
557 hash
= hexmem(&h
, sizeof(h
));
561 assert_se(strlen(hash
) == UNIT_NAME_HASH_LENGTH_CHARS
);
563 len
= UNIT_NAME_MAX
- 1 - strlen(suffix
+1) - UNIT_NAME_HASH_LENGTH_CHARS
- 2;
564 assert(len
> 0 && len
< UNIT_NAME_MAX
);
566 n
= strndup(name
, len
);
570 if (!strextend(&n
, "_", hash
, suffix
))
572 assert_se(unit_name_is_valid(n
, UNIT_NAME_PLAIN
));
579 int unit_name_from_path(const char *path
, const char *suffix
, char **ret
) {
580 _cleanup_free_
char *p
= NULL
, *s
= NULL
;
587 if (!unit_suffix_is_valid(suffix
))
590 r
= unit_name_path_escape(path
, &p
);
594 s
= strjoin(p
, suffix
);
598 if (strlen(s
) >= UNIT_NAME_MAX
) {
599 _cleanup_free_
char *n
= NULL
;
601 log_debug("Unit name \"%s\" too long, falling back to hashed unit name.", s
);
603 r
= unit_name_hash_long(s
, &n
);
607 free_and_replace(s
, n
);
610 /* Refuse if this for some other reason didn't result in a valid name */
611 if (!unit_name_is_valid(s
, UNIT_NAME_PLAIN
))
618 int unit_name_from_path_instance(const char *prefix
, const char *path
, const char *suffix
, char **ret
) {
619 _cleanup_free_
char *p
= NULL
, *s
= NULL
;
627 if (!unit_prefix_is_valid(prefix
))
630 if (!unit_suffix_is_valid(suffix
))
633 r
= unit_name_path_escape(path
, &p
);
637 s
= strjoin(prefix
, "@", p
, suffix
);
641 if (strlen(s
) >= UNIT_NAME_MAX
) /* Return a slightly more descriptive error for this specific condition */
642 return -ENAMETOOLONG
;
644 /* Refuse if this for some other reason didn't result in a valid name */
645 if (!unit_name_is_valid(s
, UNIT_NAME_INSTANCE
))
652 int unit_name_to_path(const char *name
, char **ret
) {
653 _cleanup_free_
char *prefix
= NULL
;
658 r
= unit_name_to_prefix(name
, &prefix
);
662 if (unit_name_is_hashed(name
))
663 return -ENAMETOOLONG
;
665 return unit_name_path_unescape(prefix
, ret
);
668 static bool do_escape_mangle(const char *f
, bool allow_globs
, char *t
) {
669 const char *valid_chars
;
670 bool mangled
= false;
675 /* We'll only escape the obvious characters here, to play safe.
677 * Returns true if any characters were mangled, false otherwise.
680 valid_chars
= allow_globs
? VALID_CHARS_GLOB
: VALID_CHARS_WITH_AT
;
686 } else if (!strchr(valid_chars
, *f
)) {
687 t
= do_escape_char(*f
, t
);
697 * Convert a string to a unit name. /dev/blah is converted to dev-blah.device,
698 * /blah/blah is converted to blah-blah.mount, anything else is left alone,
699 * except that @suffix is appended if a valid unit suffix is not present.
701 * If @allow_globs, globs characters are preserved. Otherwise, they are escaped.
703 int unit_name_mangle_with_suffix(
705 const char *operation
,
706 UnitNameMangle flags
,
710 _cleanup_free_
char *s
= NULL
;
711 bool mangled
, suggest_escape
= true, warn
= flags
& UNIT_NAME_MANGLE_WARN
;
718 if (isempty(name
)) /* We cannot mangle empty unit names to become valid, sorry. */
721 if (!unit_suffix_is_valid(suffix
))
724 /* Already a fully valid unit name? If so, no mangling is necessary... */
725 if (unit_name_is_valid(name
, UNIT_NAME_ANY
))
728 /* Already a fully valid globbing expression? If so, no mangling is necessary either... */
729 if (string_is_glob(name
) && in_charset(name
, VALID_CHARS_GLOB
)) {
730 if (flags
& UNIT_NAME_MANGLE_GLOB
)
732 log_full(warn
? LOG_NOTICE
: LOG_DEBUG
,
733 "Glob pattern passed%s%s, but globs are not supported for this.",
734 operation
? " " : "", strempty(operation
));
735 suggest_escape
= false;
738 if (path_is_absolute(name
)) {
739 _cleanup_free_
char *n
= NULL
;
741 r
= path_simplify_alloc(name
, &n
);
745 if (is_device_path(n
)) {
746 r
= unit_name_from_path(n
, ".device", ret
);
753 r
= unit_name_from_path(n
, ".mount", ret
);
760 s
= new(char, strlen(name
) * 4 + strlen(suffix
) + 1);
764 mangled
= do_escape_mangle(name
, flags
& UNIT_NAME_MANGLE_GLOB
, s
);
766 log_full(warn
? LOG_NOTICE
: LOG_DEBUG
,
767 "Invalid unit name \"%s\" escaped as \"%s\"%s.",
769 suggest_escape
? " (maybe you should use systemd-escape?)" : "");
771 /* Append a suffix if it doesn't have any, but only if this is not a glob, so that we can allow
772 * "foo.*" as a valid glob. */
773 if ((!(flags
& UNIT_NAME_MANGLE_GLOB
) || !string_is_glob(s
)) && unit_name_to_type(s
) < 0)
776 /* Make sure mangling didn't grow this too large (but don't do this check if globbing is allowed,
777 * since globs generally do not qualify as valid unit names) */
778 if (!FLAGS_SET(flags
, UNIT_NAME_MANGLE_GLOB
) && !unit_name_is_valid(s
, UNIT_NAME_ANY
))
793 int slice_build_parent_slice(const char *slice
, char **ret
) {
794 _cleanup_free_
char *s
= NULL
;
801 if (!slice_name_is_valid(slice
))
804 if (streq(slice
, SPECIAL_ROOT_SLICE
)) {
813 dash
= strrchr(s
, '-');
815 strcpy(dash
, ".slice");
817 r
= free_and_strdup(&s
, SPECIAL_ROOT_SLICE
);
826 int slice_build_subslice(const char *slice
, const char *name
, char **ret
) {
833 if (!slice_name_is_valid(slice
))
836 if (!unit_prefix_is_valid(name
))
839 if (streq(slice
, SPECIAL_ROOT_SLICE
))
840 subslice
= strjoin(name
, ".slice");
844 assert_se(e
= endswith(slice
, ".slice"));
846 subslice
= new(char, (e
- slice
) + 1 + strlen(name
) + 6 + 1);
850 stpcpy(stpcpy(stpcpy(mempcpy(subslice
, slice
, e
- slice
), "-"), name
), ".slice");
857 bool slice_name_is_valid(const char *name
) {
861 if (!unit_name_is_valid(name
, UNIT_NAME_PLAIN
))
864 if (streq(name
, SPECIAL_ROOT_SLICE
))
867 e
= endswith(name
, ".slice");
871 for (p
= name
; p
< e
; p
++) {
875 /* Don't allow initial dash */
879 /* Don't allow multiple dashes */
888 /* Don't allow trailing hash */
895 bool unit_name_prefix_equal(const char *a
, const char *b
) {
901 if (!unit_name_is_valid(a
, UNIT_NAME_ANY
) || !unit_name_is_valid(b
, UNIT_NAME_ANY
))
915 return memcmp_nn(a
, p
- a
, b
, q
- b
) == 0;