From 7410616cd9dbbec97cf98d75324da5cda2b2f7a2 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 30 Apr 2015 20:21:00 +0200 Subject: core: rework unit name validation and manipulation logic A variety of changes: - Make sure all our calls distuingish OOM from other errors if OOM is not the only error possible. - Be much stricter when parsing escaped paths, do not accept trailing or leading escaped slashes. - Change unit validation to take a bit mask for allowing plain names, instance names or template names or an combination thereof. - Refuse manipulating invalid unit name --- src/escape/escape.c | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) (limited to 'src/escape') diff --git a/src/escape/escape.c b/src/escape/escape.c index f2a0721861..9ccb015538 100644 --- a/src/escape/escape.c +++ b/src/escape/escape.c @@ -99,7 +99,7 @@ static int parse_argv(int argc, char *argv[]) { case ARG_TEMPLATE: - if (!unit_name_is_valid(optarg, true) || !unit_name_is_template(optarg)) { + if (!unit_name_is_valid(optarg, UNIT_NAME_TEMPLATE)) { log_error("Template name %s is not valid.", optarg); return -EINVAL; } @@ -166,22 +166,26 @@ int main(int argc, char *argv[]) { switch (arg_action) { case ACTION_ESCAPE: - if (arg_path) - e = unit_name_path_escape(*i); - else + if (arg_path) { + r = unit_name_path_escape(*i, &e); + if (r < 0) { + log_error_errno(r, "Failed to escape string: %m"); + goto finish; + } + } else { e = unit_name_escape(*i); - - if (!e) { - r = log_oom(); - goto finish; + if (!e) { + r = log_oom(); + goto finish; + } } if (arg_template) { char *x; - x = unit_name_replace_instance(arg_template, e); - if (!x) { - r = log_oom(); + r = unit_name_replace_instance(arg_template, e, &x); + if (r < 0) { + log_error_errno(r, "Failed to replace instance: %m"); goto finish; } @@ -204,20 +208,20 @@ int main(int argc, char *argv[]) { case ACTION_UNESCAPE: if (arg_path) - e = unit_name_path_unescape(*i); + r = unit_name_path_unescape(*i, &e); else - e = unit_name_unescape(*i); + r = unit_name_unescape(*i, &e); - if (!e) { - r = log_oom(); + if (r < 0) { + log_error_errno(r, "Failed to unescape string: %m"); goto finish; } break; case ACTION_MANGLE: - e = unit_name_mangle(*i, MANGLE_NOGLOB); - if (!e) { - r = log_oom(); + r = unit_name_mangle(*i, UNIT_NAME_NOGLOB, &e); + if (r < 0) { + log_error_errno(r, "Failed to mangle name: %m"); goto finish; } break; -- cgit v1.2.3-54-g00ecf