From 7f7ff280d14b2c60d92e922c66eb7f433fb633ce Mon Sep 17 00:00:00 2001 From: davidn Date: Tue, 26 Oct 1999 04:27:14 +0000 Subject: Clean up error handling in fileupdate(), which now returns 0 on success instead of a boolean. This replicated through he front-end sub-functions relating to add, delete, modify entries in passwd & group files Errno is now preserved so output of errc()/warnc() will be less obfuscated by subsequent errors when reporting the problem. Add more intelligent error handling when attempting to modify/delete NIS entries with no corresponding local database entry. [MFC to stable in a couple of weeks to keep both in sync] --- usr.sbin/pw/pw_user.c | 70 +++++++++++++++++++++++++++++++++++---------------- 1 file changed, 49 insertions(+), 21 deletions(-) (limited to 'usr.sbin/pw/pw_user.c') diff --git a/usr.sbin/pw/pw_user.c b/usr.sbin/pw/pw_user.c index bba8a01..da6c83b 100644 --- a/usr.sbin/pw/pw_user.c +++ b/usr.sbin/pw/pw_user.c @@ -102,7 +102,7 @@ static void rmskey(char const * name); int pw_user(struct userconf * cnf, int mode, struct cargs * args) { - int r, r1; + int rc; char *p = NULL; struct carg *a_name; struct carg *a_uid; @@ -357,12 +357,23 @@ pw_user(struct userconf * cnf, int mode, struct cargs * args) strncpy(home, pwd->pw_dir, sizeof home); home[sizeof home - 1] = '\0'; - if (!delpwent(pwd)) - err(EX_IOERR, "error updating passwd file"); + rc = delpwent(pwd); + if (rc == -1) + err(EX_IOERR, "user '%s' does not exist", pwd->pw_name); + else if (rc != 0) { + warnc(rc, "passwd update"); + return EX_IOERR; + } + + if (cnf->nispasswd && *cnf->nispasswd=='/') { + rc = delnispwent(cnf->nispasswd, a_name->val); + if (rc == -1) + warnx("WARNING: user '%s' does not exist in NIS passwd", pwd->pw_name); + else if (rc != 0) + warnc(rc, "WARNING: NIS passwd update"); + /* non-fatal */ + } - if (cnf->nispasswd && *cnf->nispasswd=='/' && !delnispwent(cnf->nispasswd, a_name->val)) - warn("WARNING: NIS passwd update"); - editgroups(a_name->val, NULL); pw_log(cnf, mode, W_USER, "%s(%ld) account removed", a_name->val, (long) uid); @@ -535,23 +546,40 @@ pw_user(struct userconf * cnf, int mode, struct cargs * args) getarg(args, 'P') != NULL, getarg(args, '7') != NULL); - r = r1 = 1; if (mode == M_ADD) { - r = addpwent(pwd); - if (r && cnf->nispasswd && *cnf->nispasswd=='/') - r1 = addnispwent(cnf->nispasswd, pwd); + rc = addpwent(pwd); + if (rc == -1) { + warnx("user '%s' already exists", pwd->pw_name); + return EX_IOERR; + } else if (rc != 0) { + warnc(rc, "passwd file update"); + return EX_IOERR; + } + if (cnf->nispasswd && *cnf->nispasswd=='/') { + rc = addnispwent(cnf->nispasswd, pwd); + if (rc == -1) + warnx("User '%s' already exists in NIS passwd", pwd->pw_name); + else + warnc(rc, "NIS passwd update"); + /* NOTE: we treat NIS-only update errors as non-fatal */ + } } else if (mode == M_UPDATE) { - r = chgpwent(a_name->val, pwd); - if (r && cnf->nispasswd && *cnf->nispasswd=='/') - r1 = chgnispwent(cnf->nispasswd, a_name->val, pwd); - } - - if (!r) { - warn("password update"); - return EX_IOERR; - } else if (!r1) { - warn("WARNING: NIS password update"); - /* Keep on trucking */ + rc = chgpwent(a_name->val, pwd); + if (rc == -1) { + warnx("user '%s' does not exist (NIS?)", pwd->pw_name); + return EX_IOERR; + } else if (rc != 0) { + warnc(rc, "passwd file update"); + return EX_IOERR; + } + if ( cnf->nispasswd && *cnf->nispasswd=='/') { + rc = chgnispwent(cnf->nispasswd, a_name->val, pwd); + if (rc == -1) + warn("User '%s' not found in NIS passwd", pwd->pw_name); + else + warnc(rc, "NIS passwd update"); + /* NOTE: NIS-only update errors are not fatal */ + } } /* -- cgit v1.1