* src/chgpasswd.c: Avoid assignments in comparisons.

* src/chgpasswd.c: Avoid implicit brackets.
	* src/chgpasswd.c: Fix comments to match chgpasswd (group instead of
	user's passwords are changed).

	Fix the previous ChangeLog entries regarding chgpasswd.
This commit is contained in:
nekral-guest 2007-12-28 23:14:59 +00:00
parent 9fe450e216
commit 8563319b8b
2 changed files with 47 additions and 33 deletions

View File

@ -1,8 +1,14 @@
2007-12-28 Nicolas François <nicolas.francois@centraliens.net> 2007-12-28 Nicolas François <nicolas.francois@centraliens.net>
Same changes for chgpasswd: Same changes for chgpasswd:
* src/chpasswd.c: main() split in process_flags(), check_flags(), * src/chgpasswd.c: Before pam_end(), the return value of the previous
pam API was already checked. No need to validate it again.
* src/chgpasswd.c: main() split in process_flags(), check_flags(),
check_perms(), open_files(), and close_files(). check_perms(), open_files(), and close_files().
* src/chgpasswd.c: Avoid assignments in comparisons.
* src/chgpasswd.c: Avoid implicit brackets.
* src/chgpasswd.c: Fix comments to match chgpasswd (group instead of
user's passwords are changed).
2007-12-28 Nicolas François <nicolas.francois@centraliens.net> 2007-12-28 Nicolas François <nicolas.francois@centraliens.net>

View File

@ -257,11 +257,11 @@ static void open_files (void)
* Lock the group file and open it for reading and writing. This will * Lock the group file and open it for reading and writing. This will
* bring all of the entries into memory where they may be updated. * bring all of the entries into memory where they may be updated.
*/ */
if (!gr_lock ()) { if (gr_lock () == 0) {
fprintf (stderr, _("%s: can't lock group file\n"), Prog); fprintf (stderr, _("%s: can't lock group file\n"), Prog);
exit (1); exit (1);
} }
if (!gr_open (O_RDWR)) { if (gr_open (O_RDWR) == 0) {
fprintf (stderr, _("%s: can't open group file\n"), Prog); fprintf (stderr, _("%s: can't open group file\n"), Prog);
gr_unlock (); gr_unlock ();
exit (1); exit (1);
@ -270,13 +270,13 @@ static void open_files (void)
#ifdef SHADOWGRP #ifdef SHADOWGRP
/* Do the same for the shadowed database, if it exist */ /* Do the same for the shadowed database, if it exist */
if (is_shadow_grp) { if (is_shadow_grp) {
if (!sgr_lock ()) { if (sgr_lock () == 0) {
fprintf (stderr, _("%s: can't lock gshadow file\n"), fprintf (stderr, _("%s: can't lock gshadow file\n"),
Prog); Prog);
gr_unlock (); gr_unlock ();
exit (1); exit (1);
} }
if (!sgr_open (O_RDWR)) { if (sgr_open (O_RDWR) == 0) {
fprintf (stderr, _("%s: can't open shadow file\n"), fprintf (stderr, _("%s: can't open shadow file\n"),
Prog); Prog);
gr_unlock (); gr_unlock ();
@ -294,9 +294,9 @@ static void close_files (void)
{ {
#ifdef SHADOWGRP #ifdef SHADOWGRP
if (is_shadow_grp) { if (is_shadow_grp) {
if (!sgr_close ()) { if (sgr_close () == 0) {
fprintf (stderr, fprintf (stderr,
_("%s: error updating shadow file\n"), Prog); _("%s: error updating gshadow file\n"), Prog);
gr_unlock (); gr_unlock ();
exit (1); exit (1);
} }
@ -304,8 +304,8 @@ static void close_files (void)
} }
#endif #endif
if (!gr_close ()) { if (gr_close () == 0) {
fprintf (stderr, _("%s: error updating password file\n"), Prog); fprintf (stderr, _("%s: error updating group file\n"), Prog);
exit (1); exit (1);
} }
gr_unlock (); gr_unlock ();
@ -345,12 +345,13 @@ int main (int argc, char **argv)
/* /*
* Read each line, separating the group name from the password. The * Read each line, separating the group name from the password. The
* password entry for each group will be looked up in the appropriate * group entry for each group will be looked up in the appropriate
* file (gshadow or group) and the password changed. * file (gshadow or group) and the password changed.
*/ */
while (fgets (buf, sizeof buf, stdin) != (char *) 0) { while (fgets (buf, sizeof buf, stdin) != (char *) 0) {
line++; line++;
if ((cp = strrchr (buf, '\n'))) { cp = strrchr (buf, '\n');
if (NULL != cp) {
*cp = '\0'; *cp = '\0';
} else { } else {
fprintf (stderr, _("%s: line %d: line too long\n"), fprintf (stderr, _("%s: line %d: line too long\n"),
@ -360,8 +361,8 @@ int main (int argc, char **argv)
} }
/* /*
* The groupname is the first field. It is separated from the * The group's name is the first field. It is separated from
* password with a ":" character which is replaced with a * the password with a ":" character which is replaced with a
* NUL to give the new password. The new password will then * NUL to give the new password. The new password will then
* be encrypted in the normal fashion with a new salt * be encrypted in the normal fashion with a new salt
* generated, unless the '-e' is given, in which case it is * generated, unless the '-e' is given, in which case it is
@ -369,8 +370,10 @@ int main (int argc, char **argv)
*/ */
name = buf; name = buf;
if ((cp = strchr (name, ':'))) { cp = strchr (name, ':');
*cp++ = '\0'; if (NULL != cp) {
*cp = '\0';
cp++;
} else { } else {
fprintf (stderr, fprintf (stderr,
_("%s: line %d: missing new password\n"), _("%s: line %d: missing new password\n"),
@ -383,23 +386,25 @@ int main (int argc, char **argv)
(NULL == crypt_method || (NULL == crypt_method ||
0 != strcmp(crypt_method, "NONE"))) { 0 != strcmp(crypt_method, "NONE"))) {
void *arg = NULL; void *arg = NULL;
if (md5flg) if (md5flg) {
crypt_method = "MD5"; crypt_method = "MD5";
else if (crypt_method != NULL) { } else if (crypt_method != NULL) {
if (sflg) if (sflg) {
arg = &sha_rounds; arg = &sha_rounds;
} else }
} else {
crypt_method = NULL; crypt_method = NULL;
}
cp = pw_encrypt (newpwd, cp = pw_encrypt (newpwd,
crypt_make_salt(crypt_method, arg)); crypt_make_salt(crypt_method, arg));
} }
/* /*
* Get the password file entry for this user. The user must * Get the group file entry for this group. The group must
* already exist. * already exist.
*/ */
gr = gr_locate (name); gr = gr_locate (name);
if (!gr) { if (NULL == gr) {
fprintf (stderr, fprintf (stderr,
_("%s: line %d: unknown group %s\n"), Prog, _("%s: line %d: unknown group %s\n"), Prog,
line, name); line, name);
@ -407,19 +412,19 @@ int main (int argc, char **argv)
continue; continue;
} }
#ifdef SHADOWGRP #ifdef SHADOWGRP
if (is_shadow_grp) if (is_shadow_grp) {
sg = sgr_locate (name); sg = sgr_locate (name);
else } else {
sg = NULL; sg = NULL;
}
#endif #endif
/* /*
* The freshly encrypted new password is merged into the * The freshly encrypted new password is merged into the
* user's password file entry and the last password change * group's entry.
* date is set to the current date.
*/ */
#ifdef SHADOWGRP #ifdef SHADOWGRP
if (sg) { if (NULL != sg) {
newsg = *sg; newsg = *sg;
newsg.sg_passwd = cp; newsg.sg_passwd = cp;
} else } else
@ -430,21 +435,23 @@ int main (int argc, char **argv)
} }
/* /*
* The updated password file entry is then put back and will * The updated group file entry is then put back and will
* be written to the password file later, after all the * be written to the group file later, after all the
* other entries have been updated as well. * other entries have been updated as well.
*/ */
#ifdef SHADOWGRP #ifdef SHADOWGRP
if (sg) if (NULL != sg) {
ok = sgr_update (&newsg); ok = sgr_update (&newsg);
else } else
#endif #endif
{
ok = gr_update (&newgr); ok = gr_update (&newgr);
}
if (!ok) { if (!ok) {
fprintf (stderr, fprintf (stderr,
_ _
("%s: line %d: cannot update password entry\n"), ("%s: line %d: cannot update group entry\n"),
Prog, line); Prog, line);
errors++; errors++;
continue; continue;
@ -453,7 +460,7 @@ int main (int argc, char **argv)
/* /*
* Any detected errors will cause the entire set of changes to be * Any detected errors will cause the entire set of changes to be
* aborted. Unlocking the password file will cause all of the * aborted. Unlocking the group file will cause all of the
* changes to be ignored. Otherwise the file is closed, causing the * changes to be ignored. Otherwise the file is closed, causing the
* changes to be written out all at once, and then unlocked * changes to be written out all at once, and then unlocked
* afterwards. * afterwards.
@ -462,8 +469,9 @@ int main (int argc, char **argv)
fprintf (stderr, fprintf (stderr,
_("%s: error detected, changes ignored\n"), Prog); _("%s: error detected, changes ignored\n"), Prog);
#ifdef SHADOWGRP #ifdef SHADOWGRP
if (is_shadow_grp) if (is_shadow_grp) {
sgr_unlock (); sgr_unlock ();
}
#endif #endif
gr_unlock (); gr_unlock ();
exit (1); exit (1);