diff options
Diffstat (limited to 'src/sysusers/sysusers.c')
-rw-r--r-- | src/sysusers/sysusers.c | 114 |
1 files changed, 55 insertions, 59 deletions
diff --git a/src/sysusers/sysusers.c b/src/sysusers/sysusers.c index d7ba482834..ba09727080 100644 --- a/src/sysusers/sysusers.c +++ b/src/sysusers/sysusers.c @@ -19,26 +19,26 @@ along with systemd; If not, see <http://www.gnu.org/licenses/>. ***/ -#include <pwd.h> +#include <getopt.h> #include <grp.h> -#include <shadow.h> #include <gshadow.h> -#include <getopt.h> +#include <pwd.h> +#include <shadow.h> #include <utmp.h> -#include "util.h" -#include "hashmap.h" -#include "specifier.h" -#include "path-util.h" -#include "build.h" -#include "strv.h" #include "conf-files.h" #include "copy.h" -#include "utf8.h" #include "fileio-label.h" -#include "uid-range.h" -#include "selinux-util.h" #include "formats-util.h" +#include "hashmap.h" +#include "path-util.h" +#include "selinux-util.h" +#include "specifier.h" +#include "strv.h" +#include "uid-range.h" +#include "utf8.h" +#include "util.h" +#include "smack-util.h" typedef enum ItemType { ADD_USER = 'u', @@ -207,7 +207,7 @@ static int make_backup(const char *target, const char *x) { if (r < 0) return r; - r = copy_bytes(src, fileno(dst), (off_t) -1, true); + r = copy_bytes(src, fileno(dst), (uint64_t) -1, true); if (r < 0) goto fail; @@ -353,6 +353,19 @@ static int sync_rights(FILE *from, FILE *to) { return 0; } +static int rename_and_apply_smack(const char *temp_path, const char *dest_path) { + int r = 0; + if (rename(temp_path, dest_path) < 0) + return -errno; + +#ifdef SMACK_RUN_LABEL + r = mac_smack_apply(dest_path, SMACK_ATTR_ACCESS, SMACK_FLOOR_LABEL); + if (r < 0) + return r; +#endif + return r; +} + static int write_files(void) { _cleanup_fclose_ FILE *passwd = NULL, *group = NULL, *shadow = NULL, *gshadow = NULL; @@ -699,42 +712,34 @@ static int write_files(void) { /* And make the new files count */ if (group_changed) { if (group) { - if (rename(group_tmp, group_path) < 0) { - r = -errno; + r = rename_and_apply_smack(group_tmp, group_path); + if (r < 0) goto finish; - } - free(group_tmp); - group_tmp = NULL; + group_tmp = mfree(group_tmp); } if (gshadow) { - if (rename(gshadow_tmp, gshadow_path) < 0) { - r = -errno; + r = rename_and_apply_smack(gshadow_tmp, gshadow_path); + if (r < 0) goto finish; - } - free(gshadow_tmp); - gshadow_tmp = NULL; + gshadow_tmp = mfree(gshadow_tmp); } } if (passwd) { - if (rename(passwd_tmp, passwd_path) < 0) { - r = -errno; + r = rename_and_apply_smack(passwd_tmp, passwd_path); + if (r < 0) goto finish; - } - free(passwd_tmp); - passwd_tmp = NULL; + passwd_tmp = mfree(passwd_tmp); } if (shadow) { - if (rename(shadow_tmp, shadow_path) < 0) { - r = -errno; + r = rename_and_apply_smack(shadow_tmp, shadow_path); + if (r < 0) goto finish; - } - free(shadow_tmp); - shadow_tmp = NULL; + shadow_tmp = mfree(shadow_tmp); } r = 0; @@ -891,8 +896,10 @@ static int add_user(Item *i) { i->uid = p->pw_uid; i->uid_set = true; - free(i->description); - i->description = strdup(p->pw_gecos); + r = free_and_strdup(&i->description, p->pw_gecos); + if (r < 0) + return log_oom(); + return 0; } if (!IN_SET(errno, 0, ENOENT)) @@ -1149,9 +1156,8 @@ static int process_item(Item *i) { } if (i->gid_path) { - free(j->gid_path); - j->gid_path = strdup(i->gid_path); - if (!j->gid_path) + r = free_and_strdup(&j->gid_path, i->gid_path); + if (r < 0) return log_oom(); } @@ -1383,7 +1389,7 @@ static int parse_line(const char *fname, unsigned line, const char *buffer) { /* Parse columns */ p = buffer; - r = unquote_many_words(&p, 0, &action, &name, &id, &description, &home, NULL); + r = extract_many_words(&p, NULL, EXTRACT_QUOTES, &action, &name, &id, &description, &home, NULL); if (r < 0) { log_error("[%s:%u] Syntax error.", fname, line); return r; @@ -1392,7 +1398,7 @@ static int parse_line(const char *fname, unsigned line, const char *buffer) { log_error("[%s:%u] Missing action and name columns.", fname, line); return -EINVAL; } - if (*p != 0) { + if (!isempty(p)) { log_error("[%s:%u] Trailing garbage.", fname, line); return -EINVAL; } @@ -1409,10 +1415,8 @@ static int parse_line(const char *fname, unsigned line, const char *buffer) { } /* Verify name */ - if (isempty(name) || streq(name, "-")) { - free(name); - name = NULL; - } + if (isempty(name) || streq(name, "-")) + name = mfree(name); if (name) { r = specifier_printf(name, specifier_table, NULL, &resolved_name); @@ -1428,10 +1432,8 @@ static int parse_line(const char *fname, unsigned line, const char *buffer) { } /* Verify id */ - if (isempty(id) || streq(id, "-")) { - free(id); - id = NULL; - } + if (isempty(id) || streq(id, "-")) + id = mfree(id); if (id) { r = specifier_printf(id, specifier_table, NULL, &resolved_id); @@ -1442,10 +1444,8 @@ static int parse_line(const char *fname, unsigned line, const char *buffer) { } /* Verify description */ - if (isempty(description) || streq(description, "-")) { - free(description); - description = NULL; - } + if (isempty(description) || streq(description, "-")) + description = mfree(description); if (description) { if (!valid_gecos(description)) { @@ -1455,10 +1455,8 @@ static int parse_line(const char *fname, unsigned line, const char *buffer) { } /* Verify home */ - if (isempty(home) || streq(home, "-")) { - free(home); - home = NULL; - } + if (isempty(home) || streq(home, "-")) + home = mfree(home); if (home) { if (!valid_home(home)) { @@ -1778,9 +1776,7 @@ static int parse_argv(int argc, char *argv[]) { return 0; case ARG_VERSION: - puts(PACKAGE_STRING); - puts(SYSTEMD_FEATURES); - return 0; + return version(); case ARG_ROOT: free(arg_root); |