aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Christensen <amc135@outlook.com>2021-03-27 12:02:42 -0600
committerRob Landley <rob@landley.net>2021-03-29 05:18:38 -0500
commit6a9b81b7e36d106ce017289206f4569c2c5b4df5 (patch)
tree14beb403a0ea7adfa6c2bc2442f03a65107b3346
parent0202865a1d8d5ffb8f312bf700d74fb4f43f14f5 (diff)
downloadtoybox-6a9b81b7e36d106ce017289206f4569c2c5b4df5.tar.gz
Fix system calls and get rid of tempfile
-rw-r--r--toys/pending/chsh.c18
1 files changed, 4 insertions, 14 deletions
diff --git a/toys/pending/chsh.c b/toys/pending/chsh.c
index c34660f7..bf409783 100644
--- a/toys/pending/chsh.c
+++ b/toys/pending/chsh.c
@@ -32,7 +32,6 @@ void chsh_main()
char *user, *line, *shell, *encrypted;
struct passwd *passwd_info;
struct spwd *shadow_info;
- int i;
// Get uid user information, may be discarded later
@@ -48,7 +47,7 @@ void chsh_main()
// Get a password, encrypt it, wipe it, and check it
if (mlock(toybuf, sizeof(toybuf))) perror_exit("mlock");
if (!(shadow_info = getspnam(passwd_info->pw_name))) perror_exit("getspnam");
- if (!read_password(toybuf, sizeof(toybuf), "Password: ")) xexit();
+ if (read_password(toybuf, sizeof(toybuf), "Password: ")) perror_exit("woaj"); //xexit();
if (!(encrypted = crypt(toybuf, shadow_info->sp_pwdp))) perror_exit("crypt");
memset(toybuf, 0, sizeof(toybuf));
munlock(toybuf, sizeof(toybuf)); // prevents memset from "optimizing" away.
@@ -70,18 +69,9 @@ void chsh_main()
else do line = xgetline(file); while (line && *line != '/');
if (!line) error_exit("Shell not found in '/etc/shells'");
- // Update shell and write passwd entry to tempfile
+ // Update /etc/passwd
passwd_info->pw_shell = line;
- if (!(file = tmpfile())) perror_exit("tmpfile");
- if (!putpwent(passwd_info, file)) perror_exit("putpwent");
-
- // Move passwd entry from file to string
- if (-1 == (i = ftell(file))) perror_exit("Failed to get tempfile offset");
- if (sizeof(toybuf) < i && !realloc(line, i)) perror_exit("Failed to reallocate memory");
- rewind(file);
- if (fread(line, 1, i, file) < i) perror_exit("Failed to read from tempfile");
-
- // Update /etc/passwd using that string
if (-1 == update_password("/etc/passwd", user, NULL)) perror_exit("Failed to remove passwd entry");
- if (-1 == update_password("/etc/passwd", user, line)) perror_exit("Failed to add passwd entry");
+ file = xfopen("/etc/passwd", "a");
+ if (putpwent(passwd_info, file)) perror_exit("putwent");
}