Simplify gpasswd's main():
New functions: open_files(), close_files(), update_group(). Split out from main() to simplify this (too) big function.
This commit is contained in:
parent
f429f3e38d
commit
55d581d041
@ -6,6 +6,9 @@
|
|||||||
* src/gpasswd.c: Document global variables.
|
* src/gpasswd.c: Document global variables.
|
||||||
* src/gpasswd.c: New function: process_flags(). Split the processing
|
* src/gpasswd.c: New function: process_flags(). Split the processing
|
||||||
of options out of main().
|
of options out of main().
|
||||||
|
* src/gpasswd.c: New functions: open_files(), close_files(),
|
||||||
|
update_group(). Split out from main() to simplify this (too) big
|
||||||
|
function.
|
||||||
|
|
||||||
2007-12-27 Nicolas François <nicolas.francois@centraliens.net>
|
2007-12-27 Nicolas François <nicolas.francois@centraliens.net>
|
||||||
|
|
||||||
|
222
src/gpasswd.c
222
src/gpasswd.c
@ -88,6 +88,13 @@ static void usage (void);
|
|||||||
static RETSIGTYPE catch_signals (int killed);
|
static RETSIGTYPE catch_signals (int killed);
|
||||||
static int check_list (const char *users);
|
static int check_list (const char *users);
|
||||||
static void process_flags (int argc, char **argv);
|
static void process_flags (int argc, char **argv);
|
||||||
|
static void open_files (void);
|
||||||
|
static void close_files (void);
|
||||||
|
#ifdef SHADOWGRP
|
||||||
|
static void update_group (struct group *gr, struct sgrp *sg);
|
||||||
|
#else
|
||||||
|
static void update_group (struct group *gr);
|
||||||
|
#endif
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* usage - display usage message
|
* usage - display usage message
|
||||||
@ -254,6 +261,126 @@ static void process_flags (int argc, char **argv)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* open_files - lock and open the group databases
|
||||||
|
*
|
||||||
|
* It will call exit in case of error.
|
||||||
|
*/
|
||||||
|
static void open_files (void)
|
||||||
|
{
|
||||||
|
if (gr_lock () == 0) {
|
||||||
|
fprintf (stderr, _("%s: can't get lock\n"), Prog);
|
||||||
|
SYSLOG ((LOG_WARN, "failed to get lock for /etc/group"));
|
||||||
|
#ifdef WITH_AUDIT
|
||||||
|
audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
|
||||||
|
"locking /etc/group", group, -1, 0);
|
||||||
|
#endif
|
||||||
|
exit (1);
|
||||||
|
}
|
||||||
|
#ifdef SHADOWGRP
|
||||||
|
if (is_shadowgrp && (sgr_lock () == 0)) {
|
||||||
|
fprintf (stderr, _("%s: can't get shadow lock\n"), Prog);
|
||||||
|
SYSLOG ((LOG_WARN, "failed to get lock for /etc/gshadow"));
|
||||||
|
#ifdef WITH_AUDIT
|
||||||
|
audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
|
||||||
|
"locking /etc/gshadow", group, -1, 0);
|
||||||
|
#endif
|
||||||
|
exit (1);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
if (gr_open (O_RDWR) == 0) {
|
||||||
|
fprintf (stderr, _("%s: can't open file\n"), Prog);
|
||||||
|
SYSLOG ((LOG_WARN, "cannot open /etc/group"));
|
||||||
|
#ifdef WITH_AUDIT
|
||||||
|
audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
|
||||||
|
"opening /etc/group", group, -1, 0);
|
||||||
|
#endif
|
||||||
|
exit (1);
|
||||||
|
}
|
||||||
|
#ifdef SHADOWGRP
|
||||||
|
if (is_shadowgrp && (sgr_open (O_RDWR) == 0)) {
|
||||||
|
fprintf (stderr, _("%s: can't open shadow file\n"), Prog);
|
||||||
|
SYSLOG ((LOG_WARN, "cannot open /etc/gshadow"));
|
||||||
|
#ifdef WITH_AUDIT
|
||||||
|
audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
|
||||||
|
"opening /etc/gshadow", group, -1, 0);
|
||||||
|
#endif
|
||||||
|
exit (1);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* close_files - close and unlock the group databases
|
||||||
|
*
|
||||||
|
* This cause any changes in the databases to be committed.
|
||||||
|
*
|
||||||
|
* It will call exit in case of error.
|
||||||
|
*/
|
||||||
|
static void close_files (void)
|
||||||
|
{
|
||||||
|
if (gr_close () == 0) {
|
||||||
|
fprintf (stderr, _("%s: can't re-write file\n"), Prog);
|
||||||
|
SYSLOG ((LOG_WARN, "cannot re-write /etc/group"));
|
||||||
|
#ifdef WITH_AUDIT
|
||||||
|
audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
|
||||||
|
"rewriting /etc/group", group, -1, 0);
|
||||||
|
#endif
|
||||||
|
exit (1);
|
||||||
|
}
|
||||||
|
#ifdef SHADOWGRP
|
||||||
|
if (is_shadowgrp && (sgr_close () == 0)) {
|
||||||
|
fprintf (stderr, _("%s: can't re-write shadow file\n"), Prog);
|
||||||
|
SYSLOG ((LOG_WARN, "cannot re-write /etc/gshadow"));
|
||||||
|
#ifdef WITH_AUDIT
|
||||||
|
audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
|
||||||
|
"rewriting /etc/gshadow", group, -1, 0);
|
||||||
|
#endif
|
||||||
|
exit (1);
|
||||||
|
}
|
||||||
|
if (is_shadowgrp) {
|
||||||
|
/* TODO: same logging as in open_files & for /etc/group */
|
||||||
|
sgr_unlock ();
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
if (gr_unlock () == 0) {
|
||||||
|
fprintf (stderr, _("%s: can't unlock file\n"), Prog);
|
||||||
|
#ifdef WITH_AUDIT
|
||||||
|
audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
|
||||||
|
"unlocking group file", group, -1, 0);
|
||||||
|
#endif
|
||||||
|
exit (1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef SHADOWGRP
|
||||||
|
static void update_group (struct group *gr, struct sgrp *sg)
|
||||||
|
#else
|
||||||
|
static void update_group (struct group *gr)
|
||||||
|
#endif
|
||||||
|
{
|
||||||
|
if (!gr_update (gr)) {
|
||||||
|
fprintf (stderr, _("%s: can't update entry\n"), Prog);
|
||||||
|
SYSLOG ((LOG_WARN, "cannot update /etc/group"));
|
||||||
|
#ifdef WITH_AUDIT
|
||||||
|
audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
|
||||||
|
"updating /etc/group", group, -1, 0);
|
||||||
|
#endif
|
||||||
|
exit (1);
|
||||||
|
}
|
||||||
|
#ifdef SHADOWGRP
|
||||||
|
if (is_shadowgrp && !sgr_update (sg)) {
|
||||||
|
fprintf (stderr, _("%s: can't update shadow entry\n"), Prog);
|
||||||
|
SYSLOG ((LOG_WARN, "cannot update /etc/gshadow"));
|
||||||
|
#ifdef WITH_AUDIT
|
||||||
|
audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
|
||||||
|
"updating /etc/gshadow", group, -1, 0);
|
||||||
|
#endif
|
||||||
|
exit (1);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* gpasswd - administer the /etc/group file
|
* gpasswd - administer the /etc/group file
|
||||||
*
|
*
|
||||||
@ -698,96 +825,15 @@ int main (int argc, char **argv)
|
|||||||
}
|
}
|
||||||
pwd_init ();
|
pwd_init ();
|
||||||
|
|
||||||
if (!gr_lock ()) {
|
open_files ();
|
||||||
fprintf (stderr, _("%s: can't get lock\n"), Prog);
|
|
||||||
SYSLOG ((LOG_WARN, "failed to get lock for /etc/group"));
|
|
||||||
#ifdef WITH_AUDIT
|
|
||||||
audit_logger (AUDIT_USER_CHAUTHTOK, Prog, "locking /etc/group",
|
|
||||||
group, -1, 0);
|
|
||||||
#endif
|
|
||||||
exit (1);
|
|
||||||
}
|
|
||||||
#ifdef SHADOWGRP
|
#ifdef SHADOWGRP
|
||||||
if (is_shadowgrp && !sgr_lock ()) {
|
update_group (&grent, &sgent);
|
||||||
fprintf (stderr, _("%s: can't get shadow lock\n"), Prog);
|
#else
|
||||||
SYSLOG ((LOG_WARN, "failed to get lock for /etc/gshadow"));
|
update_group (&grent);
|
||||||
#ifdef WITH_AUDIT
|
|
||||||
audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
|
|
||||||
"locking /etc/gshadow", group, -1, 0);
|
|
||||||
#endif
|
#endif
|
||||||
exit (1);
|
|
||||||
}
|
close_files ();
|
||||||
#endif
|
|
||||||
if (!gr_open (O_RDWR)) {
|
|
||||||
fprintf (stderr, _("%s: can't open file\n"), Prog);
|
|
||||||
SYSLOG ((LOG_WARN, "cannot open /etc/group"));
|
|
||||||
#ifdef WITH_AUDIT
|
|
||||||
audit_logger (AUDIT_USER_CHAUTHTOK, Prog, "opening /etc/group",
|
|
||||||
group, -1, 0);
|
|
||||||
#endif
|
|
||||||
exit (1);
|
|
||||||
}
|
|
||||||
#ifdef SHADOWGRP
|
|
||||||
if (is_shadowgrp && !sgr_open (O_RDWR)) {
|
|
||||||
fprintf (stderr, _("%s: can't open shadow file\n"), Prog);
|
|
||||||
SYSLOG ((LOG_WARN, "cannot open /etc/gshadow"));
|
|
||||||
#ifdef WITH_AUDIT
|
|
||||||
audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
|
|
||||||
"opening /etc/gshadow", group, -1, 0);
|
|
||||||
#endif
|
|
||||||
exit (1);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
if (!gr_update (&grent)) {
|
|
||||||
fprintf (stderr, _("%s: can't update entry\n"), Prog);
|
|
||||||
SYSLOG ((LOG_WARN, "cannot update /etc/group"));
|
|
||||||
#ifdef WITH_AUDIT
|
|
||||||
audit_logger (AUDIT_USER_CHAUTHTOK, Prog, "updating /etc/group",
|
|
||||||
group, -1, 0);
|
|
||||||
#endif
|
|
||||||
exit (1);
|
|
||||||
}
|
|
||||||
#ifdef SHADOWGRP
|
|
||||||
if (is_shadowgrp && !sgr_update (&sgent)) {
|
|
||||||
fprintf (stderr, _("%s: can't update shadow entry\n"), Prog);
|
|
||||||
SYSLOG ((LOG_WARN, "cannot update /etc/gshadow"));
|
|
||||||
#ifdef WITH_AUDIT
|
|
||||||
audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
|
|
||||||
"updating /etc/gshadow", group, -1, 0);
|
|
||||||
#endif
|
|
||||||
exit (1);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
if (!gr_close ()) {
|
|
||||||
fprintf (stderr, _("%s: can't re-write file\n"), Prog);
|
|
||||||
SYSLOG ((LOG_WARN, "cannot re-write /etc/group"));
|
|
||||||
#ifdef WITH_AUDIT
|
|
||||||
audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
|
|
||||||
"rewriting /etc/group", group, -1, 0);
|
|
||||||
#endif
|
|
||||||
exit (1);
|
|
||||||
}
|
|
||||||
#ifdef SHADOWGRP
|
|
||||||
if (is_shadowgrp && !sgr_close ()) {
|
|
||||||
fprintf (stderr, _("%s: can't re-write shadow file\n"), Prog);
|
|
||||||
SYSLOG ((LOG_WARN, "cannot re-write /etc/gshadow"));
|
|
||||||
#ifdef WITH_AUDIT
|
|
||||||
audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
|
|
||||||
"rewriting /etc/gshadow", group, -1, 0);
|
|
||||||
#endif
|
|
||||||
exit (1);
|
|
||||||
}
|
|
||||||
if (is_shadowgrp)
|
|
||||||
sgr_unlock ();
|
|
||||||
#endif
|
|
||||||
if (!gr_unlock ()) {
|
|
||||||
fprintf (stderr, _("%s: can't unlock file\n"), Prog);
|
|
||||||
#ifdef WITH_AUDIT
|
|
||||||
audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
|
|
||||||
"unlocking group file", group, -1, 0);
|
|
||||||
#endif
|
|
||||||
exit (1);
|
|
||||||
}
|
|
||||||
|
|
||||||
nscd_flush_cache ("group");
|
nscd_flush_cache ("group");
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user