]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/sysusers/sysusers.c
Merge pull request #1374 from olof/autoconf_gcrypt_dep
[thirdparty/systemd.git] / src / sysusers / sysusers.c
index cc4c7ef53f499b0d8ac886bbebffcd5dc33431db..aaa33354f490ce0f0021cb8a98b4940f775b5c8d 100644 (file)
@@ -80,15 +80,13 @@ static uid_t search_uid = UID_INVALID;
 static UidRange *uid_range = NULL;
 static unsigned n_uid_range = 0;
 
-#define fix_root(x) (arg_root ? strjoina(arg_root, x) : x)
-
 static int load_user_database(void) {
         _cleanup_fclose_ FILE *f = NULL;
         const char *passwd_path;
         struct passwd *pw;
         int r;
 
-        passwd_path = fix_root("/etc/passwd");
+        passwd_path = prefix_roota(arg_root, "/etc/passwd");
         f = fopen(passwd_path, "re");
         if (!f)
                 return errno == ENOENT ? 0 : -errno;
@@ -140,7 +138,7 @@ static int load_group_database(void) {
         struct group *gr;
         int r;
 
-        group_path = fix_root("/etc/group");
+        group_path = prefix_roota(arg_root, "/etc/group");
         f = fopen(group_path, "re");
         if (!f)
                 return errno == ENOENT ? 0 : -errno;
@@ -209,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;
 
@@ -369,7 +367,7 @@ static int write_files(void) {
                 _cleanup_fclose_ FILE *original = NULL;
 
                 /* First we update the actual group list file */
-                group_path = fix_root("/etc/group");
+                group_path = prefix_roota(arg_root, "/etc/group");
                 r = fopen_temporary_label("/etc/group", group_path, &group, &group_tmp);
                 if (r < 0)
                         goto finish;
@@ -448,7 +446,7 @@ static int write_files(void) {
                 }
 
                 /* OK, now also update the shadow file for the group list */
-                gshadow_path = fix_root("/etc/gshadow");
+                gshadow_path = prefix_roota(arg_root, "/etc/gshadow");
                 r = fopen_temporary_label("/etc/gshadow", gshadow_path, &gshadow, &gshadow_tmp);
                 if (r < 0)
                         goto finish;
@@ -514,7 +512,7 @@ static int write_files(void) {
                 long lstchg;
 
                 /* First we update the user database itself */
-                passwd_path = fix_root("/etc/passwd");
+                passwd_path = prefix_roota(arg_root, "/etc/passwd");
                 r = fopen_temporary_label("/etc/passwd", passwd_path, &passwd, &passwd_tmp);
                 if (r < 0)
                         goto finish;
@@ -599,7 +597,7 @@ static int write_files(void) {
                 }
 
                 /* The we update the shadow database */
-                shadow_path = fix_root("/etc/shadow");
+                shadow_path = prefix_roota(arg_root, "/etc/shadow");
                 r = fopen_temporary_label("/etc/shadow", shadow_path, &shadow, &shadow_tmp);
                 if (r < 0)
                         goto finish;
@@ -706,8 +704,7 @@ static int write_files(void) {
                                 goto finish;
                         }
 
-                        free(group_tmp);
-                        group_tmp = NULL;
+                        group_tmp = mfree(group_tmp);
                 }
                 if (gshadow) {
                         if (rename(gshadow_tmp, gshadow_path) < 0) {
@@ -715,8 +712,7 @@ static int write_files(void) {
                                 goto finish;
                         }
 
-                        free(gshadow_tmp);
-                        gshadow_tmp = NULL;
+                        gshadow_tmp = mfree(gshadow_tmp);
                 }
         }
 
@@ -726,8 +722,7 @@ static int write_files(void) {
                         goto finish;
                 }
 
-                free(passwd_tmp);
-                passwd_tmp = NULL;
+                passwd_tmp = mfree(passwd_tmp);
         }
         if (shadow) {
                 if (rename(shadow_tmp, shadow_path) < 0) {
@@ -735,8 +730,7 @@ static int write_files(void) {
                         goto finish;
                 }
 
-                free(shadow_tmp);
-                shadow_tmp = NULL;
+                shadow_tmp = mfree(shadow_tmp);
         }
 
         r = 0;
@@ -802,7 +796,7 @@ static int uid_is_ok(uid_t uid, const char *name) {
 static int root_stat(const char *p, struct stat *st) {
         const char *fix;
 
-        fix = fix_root(p);
+        fix = prefix_roota(arg_root, p);
         if (stat(fix, st) < 0)
                 return -errno;
 
@@ -893,8 +887,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))
@@ -1151,9 +1147,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();
                         }
 
@@ -1385,7 +1380,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;
@@ -1394,7 +1389,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;
         }
@@ -1411,10 +1406,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);
@@ -1430,10 +1423,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);
@@ -1444,10 +1435,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)) {
@@ -1457,10 +1446,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)) {