_cleanup_fclose_ FILE *original = NULL, *passwd = NULL;
_cleanup_(unlink_and_freep) char *passwd_tmp = NULL;
int r;
+ bool found = false;
assert(password);
i->pw_passwd = (char *) password;
if (shell)
i->pw_shell = (char *) shell;
+ found = true;
}
r = putpwent_sane(i, passwd);
return r;
} else {
+ r = fchmod(fileno(passwd), 0644);
+ if (r < 0)
+ return -errno;
+ }
+
+ if (!found) {
struct passwd root = {
.pw_name = (char *) "root",
.pw_passwd = (char *) password,
if (errno != ENOENT)
return -errno;
- r = fchmod(fileno(passwd), 0644);
- if (r < 0)
- return -errno;
-
r = putpwent_sane(&root, passwd);
if (r < 0)
return r;
_cleanup_fclose_ FILE *original = NULL, *shadow = NULL;
_cleanup_(unlink_and_freep) char *shadow_tmp = NULL;
int r;
+ bool found = false;
assert(hashed_password);
if (streq(i->sp_namp, "root")) {
i->sp_pwdp = (char *) hashed_password;
i->sp_lstchg = (long) (now(CLOCK_REALTIME) / USEC_PER_DAY);
+ found = true;
}
r = putspent_sane(i, shadow);
return r;
} else {
+ r = fchmod(fileno(shadow), 0000);
+ if (r < 0)
+ return -errno;
+ }
+
+ if (!found) {
struct spwd root = {
.sp_namp = (char*) "root",
.sp_pwdp = (char *) hashed_password,
if (errno != ENOENT)
return -errno;
- r = fchmod(fileno(shadow), 0000);
- if (r < 0)
- return -errno;
-
r = putspent_sane(&root, shadow);
if (r < 0)
return r;
grep -q "^root:x:0:0:" "$ROOT/etc/passwd"
grep -q "^root:[^!*]" "$ROOT/etc/shadow"
rm -fv "$ROOT/etc/passwd" "$ROOT/etc/shadow" root.passwd
+# Make sure the root password is set if /etc/passwd and /etc/shadow exist but
+# don't have a root entry.
+touch "$ROOT/etc/passwd" "$ROOT/etc/shadow"
+systemd-firstboot --root="$ROOT" --root-password=foo
+grep -q "^root:x:0:0:" "$ROOT/etc/passwd"
+grep -q "^root:[^!*]" "$ROOT/etc/shadow"
+rm -fv "$ROOT/etc/passwd" "$ROOT/etc/shadow"
# If /etc/passwd and /etc/shadow exist, they will only be updated if the shadow
# password is !unprovisioned.
echo "root:x:0:0:root:/root:/bin/sh" >"$ROOT/etc/passwd"