From 530ff00378e7fc235e5ea51067c77398d575141e Mon Sep 17 00:00:00 2001 From: Baptiste Daroussin Date: Tue, 30 Oct 2012 08:00:53 +0000 Subject: Teach pw(8) about how to use pw/gr API to reduce code duplication MFC after: 2 months --- pw/pw_nis.c | 52 +++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 37 insertions(+), 15 deletions(-) (limited to 'pw/pw_nis.c') diff --git a/pw/pw_nis.c b/pw/pw_nis.c index 74a3ed0..af5901a 100644 --- a/pw/pw_nis.c +++ b/pw/pw_nis.c @@ -33,40 +33,62 @@ static const char rcsid[] = #include #include #include +#include +#include +#include #include "pw.h" static int -pw_nisupdate(const char * path, struct passwd * pwd, char const * user, int mode) +pw_nisupdate(const char * path, struct passwd * pwd, char const * user) { - char pfx[32]; - char pwbuf[PWBUFSZ]; - int l = sprintf(pfx, "%s:", user); + int pfd, tfd; + struct passwd *pw = NULL; + struct passwd *old_pw = NULL; - /* - * Update the passwd file first - */ - if (pwd == NULL) - *pwbuf = '\0'; - else - fmtpwentry(pwbuf, pwd, PWF_MASTER); - return fileupdate(path, 0600, pwbuf, pfx, l, mode) != 0; + if (pwd != NULL) + pw = pw_dup(pwd); + + if (user != NULL) + old_pw = GETPWNAM(user); + + if (pw_init(NULL, path)) + err(1,"pw_init()"); + if ((pfd = pw_lock()) == -1) { + pw_fini(); + err(1, "pw_lock()"); + } + if ((tfd = pw_tmp(-1)) == -1) { + pw_fini(); + err(1, "pw_tmp()"); + } + if (pw_copy(pfd, tfd, pw, old_pw) == -1) { + pw_fini(); + err(1, "pw_copy()"); + } + if (rename(pw_tempname(), path) == -1) + err(1, "rename()"); + + free(pw); + pw_fini(); + + return (0); } int addnispwent(const char *path, struct passwd * pwd) { - return pw_nisupdate(path, pwd, pwd->pw_name, UPD_CREATE); + return pw_nisupdate(path, pwd, NULL); } int chgnispwent(const char *path, char const * login, struct passwd * pwd) { - return pw_nisupdate(path, pwd, login, UPD_REPLACE); + return pw_nisupdate(path, pwd, login); } int delnispwent(const char *path, const char *login) { - return pw_nisupdate(path, NULL, login, UPD_DELETE); + return pw_nisupdate(path, NULL, login); } -- cgit v1.2.3-56-ge451 From ef34619dbd59da6485c17838c174b91c7938c05a Mon Sep 17 00:00:00 2001 From: Baptiste Daroussin Date: Tue, 20 Nov 2012 10:59:41 +0000 Subject: Correctly set the password file mode after renaming in NIS mode --- pw/pw_nis.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'pw/pw_nis.c') diff --git a/pw/pw_nis.c b/pw/pw_nis.c index af5901a..7af2bef 100644 --- a/pw/pw_nis.c +++ b/pw/pw_nis.c @@ -68,6 +68,8 @@ pw_nisupdate(const char * path, struct passwd * pwd, char const * user) } if (rename(pw_tempname(), path) == -1) err(1, "rename()"); + if (chmod(path, 0644) == -1) + err(1, "chmod()"); free(pw); pw_fini(); -- cgit v1.2.3-56-ge451 From f1c19efaaef09572d044b5d2656c4ee623dea092 Mon Sep 17 00:00:00 2001 From: Baptiste Daroussin Date: Tue, 20 Nov 2012 14:05:46 +0000 Subject: In NIS mode first chmod(2) the temporary file and is succeed then rename(2) --- pw/pw_nis.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'pw/pw_nis.c') diff --git a/pw/pw_nis.c b/pw/pw_nis.c index 7af2bef..918fc30 100644 --- a/pw/pw_nis.c +++ b/pw/pw_nis.c @@ -66,10 +66,10 @@ pw_nisupdate(const char * path, struct passwd * pwd, char const * user) pw_fini(); err(1, "pw_copy()"); } + if (chmod(pw_tempname(), 0644) == -1) + err(1, "chmod()"); if (rename(pw_tempname(), path) == -1) err(1, "rename()"); - if (chmod(path, 0644) == -1) - err(1, "chmod()"); free(pw); pw_fini(); -- cgit v1.2.3-56-ge451