New functions: process_flags(), check_flags(),

check_perms(). Split out of main().
This commit is contained in:
nekral-guest 2007-12-28 22:05:51 +00:00
parent dc1dccd9e2
commit 566b357f99
2 changed files with 124 additions and 93 deletions

View File

@ -2,6 +2,8 @@
* src/chpasswd.c: Before pam_end(), the return value of the previous
pam API was already checked. No need to validate it again.
* src/chpasswd.c: New functions: process_flags(), check_flags(),
check_perms(). Split out of main().
2007-12-28 Nicolas François <nicolas.francois@centraliens.net>

View File

@ -61,6 +61,9 @@ static int is_shadow_pwd;
/* local function prototypes */
static void usage (void);
static void process_flags (int argc, char **argv);
static void check_flags (void);
static void check_perms (void);
/*
* usage - display usage message and exit
@ -89,34 +92,12 @@ static void usage (void)
exit (E_USAGE);
}
int main (int argc, char **argv)
{
char buf[BUFSIZ];
char *name;
char *newpwd;
char *cp;
const struct spwd *sp;
struct spwd newsp;
const struct passwd *pw;
struct passwd newpw;
int errors = 0;
int line = 0;
long now = time ((long *) 0) / (24L * 3600L);
int ok;
#ifdef USE_PAM
pam_handle_t *pamh = NULL;
int retval;
#endif
Prog = Basename (argv[0]);
setlocale (LC_ALL, "");
bindtextdomain (PACKAGE, LOCALEDIR);
textdomain (PACKAGE);
/*
* process_flags - parse the command line options
*
* It will not return if an error is encountered.
*/
static void process_flags (int argc, char **argv)
{
int option_index = 0;
int c;
@ -131,15 +112,13 @@ int main (int argc, char **argv)
{NULL, 0, NULL, '\0'}
};
while ((c =
getopt_long (argc, argv,
while ((c = getopt_long (argc, argv,
#ifdef USE_SHA_CRYPT
"c:ehms:",
#else
"c:ehm",
#endif
long_options,
&option_index)) != -1) {
long_options, &option_index)) != -1) {
switch (c) {
case 'c':
cflg = 1;
@ -173,15 +152,25 @@ int main (int argc, char **argv)
break;
}
}
}
/* validate options */
check_flags ();
}
/*
* check_flags - check flags and parameters consistency
*
* It will not return if an error is encountered.
*/
static void check_flags ()
{
if (sflg && !cflg) {
fprintf (stderr,
_("%s: %s flag is ONLY allowed with the %s flag\n"),
Prog, "-s", "-c");
usage ();
}
if ((eflg && (md5flg || cflg)) ||
(md5flg && cflg)) {
fprintf (stderr,
@ -189,13 +178,14 @@ int main (int argc, char **argv)
Prog);
usage ();
}
if (cflg) {
if ( 0 != strcmp (crypt_method, "DES")
&& 0 != strcmp (crypt_method, "MD5")
&& 0 != strcmp (crypt_method, "NONE")
if ( (0 != strcmp (crypt_method, "DES"))
&& (0 != strcmp (crypt_method, "MD5"))
&& (0 != strcmp (crypt_method, "NONE"))
#ifdef USE_SHA_CRYPT
&& 0 != strcmp (crypt_method, "SHA256")
&& 0 != strcmp (crypt_method, "SHA512")
&& (0 != strcmp (crypt_method, "SHA256"))
&& (0 != strcmp (crypt_method, "SHA512"))
#endif
) {
fprintf (stderr,
@ -204,11 +194,24 @@ int main (int argc, char **argv)
usage ();
}
}
}
#ifdef USE_PAM
retval = PAM_SUCCESS;
/*
* check_perms - check if the caller is allowed to add a group
*
* With PAM support, the setuid bit can be set on groupadd to allow
* non-root users to groups.
* Without PAM support, only users who can write in the group databases
* can add groups.
*
* It will not return if the user is not allowed.
*/
static void check_perms (void)
{
#ifdef USE_PAM
pam_handle_t *pamh = NULL;
int retval = PAM_SUCCESS;
struct passwd *pampw;
pampw = getpwuid (getuid ()); /* local, no need for xgetpwuid */
if (pampw == NULL) {
@ -216,9 +219,7 @@ int main (int argc, char **argv)
}
if (retval == PAM_SUCCESS) {
retval = pam_start ("chpasswd", pampw->pw_name,
&conv, &pamh);
}
retval = pam_start ("chpasswd", pampw->pw_name, &conv, &pamh);
}
if (retval == PAM_SUCCESS) {
@ -240,6 +241,34 @@ int main (int argc, char **argv)
exit (1);
}
#endif /* USE_PAM */
}
int main (int argc, char **argv)
{
char buf[BUFSIZ];
char *name;
char *newpwd;
char *cp;
const struct spwd *sp;
struct spwd newsp;
const struct passwd *pw;
struct passwd newpw;
int errors = 0;
int line = 0;
long now = time ((long *) 0) / (24L * 3600L);
int ok;
Prog = Basename (argv[0]);
setlocale (LC_ALL, "");
bindtextdomain (PACKAGE, LOCALEDIR);
textdomain (PACKAGE);
process_flags (argc, argv);
check_perms ();
/*
* Lock the password file and open it for reading. This will bring