From 6a9b81b7e36d106ce017289206f4569c2c5b4df5 Mon Sep 17 00:00:00 2001 From: Michael Christensen Date: Sat, 27 Mar 2021 12:02:42 -0600 Subject: Fix system calls and get rid of tempfile --- toys/pending/chsh.c | 18 ++++-------------- 1 file 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"); } -- cgit v1.2.3