diff options
author | bapt <bapt@FreeBSD.org> | 2012-10-30 08:00:53 +0000 |
---|---|---|
committer | bapt <bapt@FreeBSD.org> | 2012-10-30 08:00:53 +0000 |
commit | cfea627c58f70cc4126131afee74d6b05a61cc6a (patch) | |
tree | f8fbed3c12f4f1af29e9e5a6673aebe65379a86c /usr.sbin/pw/pw_nis.c | |
parent | 730fb9d66a265a556977aeb09ba87ab28e754cba (diff) | |
download | FreeBSD-src-cfea627c58f70cc4126131afee74d6b05a61cc6a.zip FreeBSD-src-cfea627c58f70cc4126131afee74d6b05a61cc6a.tar.gz |
Teach pw(8) about how to use pw/gr API to reduce code duplication
MFC after: 2 months
Diffstat (limited to 'usr.sbin/pw/pw_nis.c')
-rw-r--r-- | usr.sbin/pw/pw_nis.c | 52 |
1 files changed, 37 insertions, 15 deletions
diff --git a/usr.sbin/pw/pw_nis.c b/usr.sbin/pw/pw_nis.c index 74a3ed0..af5901a 100644 --- a/usr.sbin/pw/pw_nis.c +++ b/usr.sbin/pw/pw_nis.c @@ -33,40 +33,62 @@ static const char rcsid[] = #include <stdlib.h> #include <string.h> #include <sys/types.h> +#include <err.h> +#include <pwd.h> +#include <libutil.h> #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); } |