2007-10-07 17:17:22 +05:30
|
|
|
/*
|
2008-04-27 06:10:09 +05:30
|
|
|
* Copyright (c) 1990 - 1994, Julianne Frances Haugh
|
|
|
|
* Copyright (c) 2006 , Tomasz Kłoczko
|
|
|
|
* Copyright (c) 2006 , Jonas Meurer
|
|
|
|
* Copyright (c) 2007 - 2008, Nicolas François
|
2007-10-07 17:17:22 +05:30
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2008-04-27 06:10:09 +05:30
|
|
|
* 3. The name of the copyright holders or contributors may not be used to
|
|
|
|
* endorse or promote products derived from this software without
|
|
|
|
* specific prior written permission.
|
2007-10-07 17:17:22 +05:30
|
|
|
*
|
2008-04-27 06:10:09 +05:30
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
|
|
|
|
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2007-10-07 17:17:22 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2007-11-11 05:16:11 +05:30
|
|
|
#ident "$Id$"
|
2007-10-07 17:17:22 +05:30
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#ifdef USE_PAM
|
|
|
|
#include "pam_defs.h"
|
|
|
|
#endif /* USE_PAM */
|
|
|
|
#include "defines.h"
|
|
|
|
#include "nscd.h"
|
|
|
|
#include "prototypes.h"
|
|
|
|
#include "groupio.h"
|
2007-10-07 17:17:33 +05:30
|
|
|
#ifdef SHADOWGRP
|
2007-10-07 17:17:22 +05:30
|
|
|
#include "sgroupio.h"
|
2007-10-07 17:17:33 +05:30
|
|
|
#endif
|
2007-10-07 17:17:22 +05:30
|
|
|
/*
|
|
|
|
* Global variables
|
|
|
|
*/
|
|
|
|
static char *Prog;
|
2008-06-10 02:26:03 +05:30
|
|
|
static bool cflg = false;
|
|
|
|
static bool eflg = false;
|
|
|
|
static bool md5flg = false;
|
|
|
|
static bool sflg = false;
|
2007-11-20 15:03:52 +05:30
|
|
|
|
2008-01-06 17:56:20 +05:30
|
|
|
static const char *crypt_method = NULL;
|
2007-11-24 01:39:57 +05:30
|
|
|
static long sha_rounds = 5000;
|
2007-10-07 17:17:22 +05:30
|
|
|
|
2007-10-07 17:17:33 +05:30
|
|
|
#ifdef SHADOWGRP
|
2008-06-10 02:26:03 +05:30
|
|
|
static bool is_shadow_grp;
|
2007-10-07 17:17:33 +05:30
|
|
|
#endif
|
2007-10-07 17:17:22 +05:30
|
|
|
|
2007-12-31 10:27:54 +05:30
|
|
|
#ifdef USE_PAM
|
|
|
|
static pam_handle_t *pamh = NULL;
|
|
|
|
#endif
|
|
|
|
|
2007-10-07 17:17:22 +05:30
|
|
|
/* local function prototypes */
|
|
|
|
static void usage (void);
|
2007-12-29 04:24:35 +05:30
|
|
|
static void process_flags (int argc, char **argv);
|
|
|
|
static void check_flags (void);
|
|
|
|
static void check_perms (void);
|
|
|
|
static void open_files (void);
|
|
|
|
static void close_files (void);
|
2007-10-07 17:17:22 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* usage - display usage message and exit
|
|
|
|
*/
|
|
|
|
static void usage (void)
|
|
|
|
{
|
2007-11-24 01:39:57 +05:30
|
|
|
fprintf (stderr, _("Usage: %s [options]\n"
|
2007-12-29 04:29:17 +05:30
|
|
|
"\n"
|
|
|
|
"Options:\n"
|
|
|
|
" -c, --crypt-method the crypt method (one of %s)\n"
|
|
|
|
" -e, --encrypted supplied passwords are encrypted\n"
|
|
|
|
" -h, --help display this help message and exit\n"
|
|
|
|
" -m, --md5 encrypt the clear text password using\n"
|
|
|
|
" the MD5 algorithm\n"
|
|
|
|
"%s"
|
|
|
|
"\n"),
|
|
|
|
Prog,
|
* configure.in: New configure option: --with-sha-crypt enabled by
default. Keeping the feature enabled is safe. Disabling it permits
to disable the references to the SHA256 and SHA512 password
encryption algorithms from the usage help and manuals (in addition
to the support for these algorithms in the code).
* libmisc/obscure.c, libmisc/salt.c, src/newusers.c,
src/chpasswd.c, src/chgpasswd.c, src/passwd.c: ENCRYPT_METHOD is
always supported in login.defs. Remove the ENCRYPTMETHOD_SELECT
preprocessor condition.
* libmisc/obscure.c, libmisc/salt.c, src/newusers.c,
src/chpasswd.c, src/chgpasswd.c, src/passwd.c: Disable SHA256 and
SHA512 if USE_SHA_CRYPT is not defined (this corresponds to a
subset of the ENCRYPTMETHOD_SELECT sections).
2007-11-24 18:38:08 +05:30
|
|
|
#ifndef USE_SHA_CRYPT
|
2007-12-29 04:29:17 +05:30
|
|
|
"NONE DES MD5", ""
|
2007-11-20 15:03:52 +05:30
|
|
|
#else
|
2007-12-29 04:29:17 +05:30
|
|
|
"NONE DES MD5 SHA256 SHA512",
|
|
|
|
_(" -s, --sha-rounds number of SHA rounds for the SHA*\n"
|
|
|
|
" crypt algorithms\n")
|
2007-11-20 15:03:52 +05:30
|
|
|
#endif
|
2007-12-29 04:29:17 +05:30
|
|
|
);
|
2007-10-07 17:17:22 +05:30
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
2007-12-29 04:24:35 +05:30
|
|
|
/*
|
|
|
|
* process_flags - parse the command line options
|
|
|
|
*
|
|
|
|
* It will not return if an error is encountered.
|
|
|
|
*/
|
|
|
|
static void process_flags (int argc, char **argv)
|
2007-10-07 17:17:22 +05:30
|
|
|
{
|
2007-12-29 04:24:35 +05:30
|
|
|
int option_index = 0;
|
|
|
|
int c;
|
|
|
|
static struct option long_options[] = {
|
|
|
|
{"crypt-method", required_argument, NULL, 'c'},
|
|
|
|
{"encrypted", no_argument, NULL, 'e'},
|
|
|
|
{"help", no_argument, NULL, 'h'},
|
|
|
|
{"md5", no_argument, NULL, 'm'},
|
* configure.in: New configure option: --with-sha-crypt enabled by
default. Keeping the feature enabled is safe. Disabling it permits
to disable the references to the SHA256 and SHA512 password
encryption algorithms from the usage help and manuals (in addition
to the support for these algorithms in the code).
* libmisc/obscure.c, libmisc/salt.c, src/newusers.c,
src/chpasswd.c, src/chgpasswd.c, src/passwd.c: ENCRYPT_METHOD is
always supported in login.defs. Remove the ENCRYPTMETHOD_SELECT
preprocessor condition.
* libmisc/obscure.c, libmisc/salt.c, src/newusers.c,
src/chpasswd.c, src/chgpasswd.c, src/passwd.c: Disable SHA256 and
SHA512 if USE_SHA_CRYPT is not defined (this corresponds to a
subset of the ENCRYPTMETHOD_SELECT sections).
2007-11-24 18:38:08 +05:30
|
|
|
#ifdef USE_SHA_CRYPT
|
2007-12-29 04:24:35 +05:30
|
|
|
{"sha-rounds", required_argument, NULL, 's'},
|
2007-11-24 05:46:41 +05:30
|
|
|
#endif
|
2007-12-29 04:24:35 +05:30
|
|
|
{NULL, 0, NULL, '\0'}
|
|
|
|
};
|
2007-10-07 17:17:22 +05:30
|
|
|
|
2007-12-29 04:24:35 +05:30
|
|
|
while ((c = getopt_long (argc, argv,
|
* configure.in: New configure option: --with-sha-crypt enabled by
default. Keeping the feature enabled is safe. Disabling it permits
to disable the references to the SHA256 and SHA512 password
encryption algorithms from the usage help and manuals (in addition
to the support for these algorithms in the code).
* libmisc/obscure.c, libmisc/salt.c, src/newusers.c,
src/chpasswd.c, src/chgpasswd.c, src/passwd.c: ENCRYPT_METHOD is
always supported in login.defs. Remove the ENCRYPTMETHOD_SELECT
preprocessor condition.
* libmisc/obscure.c, libmisc/salt.c, src/newusers.c,
src/chpasswd.c, src/chgpasswd.c, src/passwd.c: Disable SHA256 and
SHA512 if USE_SHA_CRYPT is not defined (this corresponds to a
subset of the ENCRYPTMETHOD_SELECT sections).
2007-11-24 18:38:08 +05:30
|
|
|
#ifdef USE_SHA_CRYPT
|
2007-12-29 04:24:35 +05:30
|
|
|
"c:ehms:",
|
2007-11-24 05:46:41 +05:30
|
|
|
#else
|
2007-12-29 04:24:35 +05:30
|
|
|
"c:ehm",
|
2007-11-24 05:46:41 +05:30
|
|
|
#endif
|
2007-12-29 04:24:35 +05:30
|
|
|
long_options, &option_index)) != -1) {
|
|
|
|
switch (c) {
|
|
|
|
case 'c':
|
2008-06-10 02:26:03 +05:30
|
|
|
cflg = true;
|
2007-12-29 04:24:35 +05:30
|
|
|
crypt_method = optarg;
|
|
|
|
break;
|
|
|
|
case 'e':
|
2008-06-10 02:26:03 +05:30
|
|
|
eflg = true;
|
2007-12-29 04:24:35 +05:30
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
usage ();
|
|
|
|
break;
|
|
|
|
case 'm':
|
2008-06-10 02:26:03 +05:30
|
|
|
md5flg = true;
|
2007-12-29 04:24:35 +05:30
|
|
|
break;
|
* configure.in: New configure option: --with-sha-crypt enabled by
default. Keeping the feature enabled is safe. Disabling it permits
to disable the references to the SHA256 and SHA512 password
encryption algorithms from the usage help and manuals (in addition
to the support for these algorithms in the code).
* libmisc/obscure.c, libmisc/salt.c, src/newusers.c,
src/chpasswd.c, src/chgpasswd.c, src/passwd.c: ENCRYPT_METHOD is
always supported in login.defs. Remove the ENCRYPTMETHOD_SELECT
preprocessor condition.
* libmisc/obscure.c, libmisc/salt.c, src/newusers.c,
src/chpasswd.c, src/chgpasswd.c, src/passwd.c: Disable SHA256 and
SHA512 if USE_SHA_CRYPT is not defined (this corresponds to a
subset of the ENCRYPTMETHOD_SELECT sections).
2007-11-24 18:38:08 +05:30
|
|
|
#ifdef USE_SHA_CRYPT
|
2007-12-29 04:24:35 +05:30
|
|
|
case 's':
|
2008-06-10 02:26:03 +05:30
|
|
|
sflg = true;
|
|
|
|
if (getlong(optarg, &sha_rounds) != 0) {
|
2007-12-29 04:24:35 +05:30
|
|
|
fprintf (stderr,
|
|
|
|
_("%s: invalid numeric argument '%s'\n"),
|
|
|
|
Prog, optarg);
|
2007-10-07 17:17:22 +05:30
|
|
|
usage ();
|
|
|
|
}
|
2007-12-29 04:24:35 +05:30
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
case 0:
|
|
|
|
/* long option */
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage ();
|
|
|
|
break;
|
2007-10-07 17:17:22 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-20 15:03:52 +05:30
|
|
|
/* validate options */
|
2007-12-29 04:24:35 +05:30
|
|
|
check_flags ();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* check_flags - check flags and parameters consistency
|
|
|
|
*
|
|
|
|
* It will not return if an error is encountered.
|
|
|
|
*/
|
|
|
|
static void check_flags (void)
|
|
|
|
{
|
2007-11-20 15:03:52 +05:30
|
|
|
if (sflg && !cflg) {
|
|
|
|
fprintf (stderr,
|
2007-12-29 04:29:17 +05:30
|
|
|
_("%s: %s flag is ONLY allowed with the %s flag\n"),
|
|
|
|
Prog, "-s", "-c");
|
2007-11-20 15:03:52 +05:30
|
|
|
usage ();
|
|
|
|
}
|
2007-12-29 04:24:35 +05:30
|
|
|
|
2007-11-20 18:39:55 +05:30
|
|
|
if ((eflg && (md5flg || cflg)) ||
|
2007-11-21 01:30:16 +05:30
|
|
|
(md5flg && cflg)) {
|
2007-11-20 15:03:52 +05:30
|
|
|
fprintf (stderr,
|
2007-12-29 04:29:17 +05:30
|
|
|
_("%s: the -c, -e, and -m flags are exclusive\n"),
|
|
|
|
Prog);
|
2007-11-20 15:03:52 +05:30
|
|
|
usage ();
|
|
|
|
}
|
2007-12-29 04:24:35 +05:30
|
|
|
|
2007-11-20 15:03:52 +05:30
|
|
|
if (cflg) {
|
2007-11-24 02:21:43 +05:30
|
|
|
if ( 0 != strcmp (crypt_method, "DES")
|
|
|
|
&& 0 != strcmp (crypt_method, "MD5")
|
|
|
|
&& 0 != strcmp (crypt_method, "NONE")
|
* configure.in: New configure option: --with-sha-crypt enabled by
default. Keeping the feature enabled is safe. Disabling it permits
to disable the references to the SHA256 and SHA512 password
encryption algorithms from the usage help and manuals (in addition
to the support for these algorithms in the code).
* libmisc/obscure.c, libmisc/salt.c, src/newusers.c,
src/chpasswd.c, src/chgpasswd.c, src/passwd.c: ENCRYPT_METHOD is
always supported in login.defs. Remove the ENCRYPTMETHOD_SELECT
preprocessor condition.
* libmisc/obscure.c, libmisc/salt.c, src/newusers.c,
src/chpasswd.c, src/chgpasswd.c, src/passwd.c: Disable SHA256 and
SHA512 if USE_SHA_CRYPT is not defined (this corresponds to a
subset of the ENCRYPTMETHOD_SELECT sections).
2007-11-24 18:38:08 +05:30
|
|
|
#ifdef USE_SHA_CRYPT
|
2007-11-24 02:21:43 +05:30
|
|
|
&& 0 != strcmp (crypt_method, "SHA256")
|
|
|
|
&& 0 != strcmp (crypt_method, "SHA512")
|
2007-11-20 15:03:52 +05:30
|
|
|
#endif
|
|
|
|
) {
|
|
|
|
fprintf (stderr,
|
2007-11-24 01:39:57 +05:30
|
|
|
_("%s: unsupported crypt method: %s\n"),
|
|
|
|
Prog, crypt_method);
|
2007-11-20 15:03:52 +05:30
|
|
|
usage ();
|
|
|
|
}
|
|
|
|
}
|
2007-12-29 04:24:35 +05:30
|
|
|
}
|
2007-11-20 15:03:52 +05:30
|
|
|
|
2007-12-29 04:24:35 +05:30
|
|
|
/*
|
|
|
|
* check_perms - check if the caller is allowed to add a group
|
|
|
|
*
|
2008-01-02 01:23:02 +05:30
|
|
|
* With PAM support, the setuid bit can be set on chgpasswd to allow
|
2007-12-29 04:24:35 +05:30
|
|
|
* 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)
|
|
|
|
{
|
2007-10-07 17:17:22 +05:30
|
|
|
#ifdef USE_PAM
|
2007-12-29 04:24:35 +05:30
|
|
|
int retval = PAM_SUCCESS;
|
|
|
|
struct passwd *pampw;
|
2007-12-31 10:27:54 +05:30
|
|
|
|
2007-12-29 04:24:35 +05:30
|
|
|
pampw = getpwuid (getuid ()); /* local, no need for xgetpwuid */
|
|
|
|
if (pampw == NULL) {
|
|
|
|
retval = PAM_USER_UNKNOWN;
|
|
|
|
}
|
2007-10-07 17:17:22 +05:30
|
|
|
|
2007-12-29 04:24:35 +05:30
|
|
|
if (retval == PAM_SUCCESS) {
|
|
|
|
retval = pam_start ("chgpasswd", pampw->pw_name, &conv, &pamh);
|
2007-10-07 17:17:22 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if (retval == PAM_SUCCESS) {
|
|
|
|
retval = pam_authenticate (pamh, 0);
|
|
|
|
if (retval != PAM_SUCCESS) {
|
|
|
|
pam_end (pamh, retval);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (retval == PAM_SUCCESS) {
|
|
|
|
retval = pam_acct_mgmt (pamh, 0);
|
|
|
|
if (retval != PAM_SUCCESS) {
|
|
|
|
pam_end (pamh, retval);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (retval != PAM_SUCCESS) {
|
|
|
|
fprintf (stderr, _("%s: PAM authentication failed\n"), Prog);
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
#endif /* USE_PAM */
|
2007-12-29 04:24:35 +05:30
|
|
|
}
|
2007-10-07 17:17:22 +05:30
|
|
|
|
2007-12-29 04:24:35 +05:30
|
|
|
/*
|
|
|
|
* open_files - lock and open the group databases
|
|
|
|
*/
|
|
|
|
static void open_files (void)
|
|
|
|
{
|
2007-10-07 17:17:22 +05:30
|
|
|
/*
|
2007-12-29 04:24:35 +05:30
|
|
|
* 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.
|
2007-10-07 17:17:22 +05:30
|
|
|
*/
|
2007-12-29 04:44:59 +05:30
|
|
|
if (gr_lock () == 0) {
|
2007-10-07 17:17:22 +05:30
|
|
|
fprintf (stderr, _("%s: can't lock group file\n"), Prog);
|
|
|
|
exit (1);
|
|
|
|
}
|
2007-12-29 04:44:59 +05:30
|
|
|
if (gr_open (O_RDWR) == 0) {
|
2007-10-07 17:17:22 +05:30
|
|
|
fprintf (stderr, _("%s: can't open group file\n"), Prog);
|
|
|
|
gr_unlock ();
|
|
|
|
exit (1);
|
|
|
|
}
|
2007-12-29 04:24:35 +05:30
|
|
|
|
2007-10-07 17:17:33 +05:30
|
|
|
#ifdef SHADOWGRP
|
2007-12-29 04:24:35 +05:30
|
|
|
/* Do the same for the shadowed database, if it exist */
|
2007-10-07 17:17:33 +05:30
|
|
|
if (is_shadow_grp) {
|
2007-12-29 04:44:59 +05:30
|
|
|
if (sgr_lock () == 0) {
|
2007-10-07 17:17:22 +05:30
|
|
|
fprintf (stderr, _("%s: can't lock gshadow file\n"),
|
2007-12-29 04:29:17 +05:30
|
|
|
Prog);
|
2007-10-07 17:17:22 +05:30
|
|
|
gr_unlock ();
|
|
|
|
exit (1);
|
|
|
|
}
|
2007-12-29 04:44:59 +05:30
|
|
|
if (sgr_open (O_RDWR) == 0) {
|
2007-10-07 17:17:22 +05:30
|
|
|
fprintf (stderr, _("%s: can't open shadow file\n"),
|
2007-12-29 04:29:17 +05:30
|
|
|
Prog);
|
2007-10-07 17:17:22 +05:30
|
|
|
gr_unlock ();
|
|
|
|
sgr_unlock ();
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
}
|
2007-10-07 17:17:33 +05:30
|
|
|
#endif
|
2007-12-29 04:24:35 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* close_files - close and unlock the group databases
|
|
|
|
*/
|
|
|
|
static void close_files (void)
|
|
|
|
{
|
|
|
|
#ifdef SHADOWGRP
|
|
|
|
if (is_shadow_grp) {
|
2007-12-29 04:44:59 +05:30
|
|
|
if (sgr_close () == 0) {
|
2007-12-29 04:24:35 +05:30
|
|
|
fprintf (stderr,
|
2007-12-29 04:44:59 +05:30
|
|
|
_("%s: error updating gshadow file\n"), Prog);
|
2007-12-29 04:24:35 +05:30
|
|
|
gr_unlock ();
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
sgr_unlock ();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-12-29 04:44:59 +05:30
|
|
|
if (gr_close () == 0) {
|
|
|
|
fprintf (stderr, _("%s: error updating group file\n"), Prog);
|
2007-12-29 04:24:35 +05:30
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
gr_unlock ();
|
|
|
|
}
|
|
|
|
|
|
|
|
int main (int argc, char **argv)
|
|
|
|
{
|
|
|
|
char buf[BUFSIZ];
|
|
|
|
char *name;
|
|
|
|
char *newpwd;
|
|
|
|
char *cp;
|
|
|
|
|
|
|
|
#ifdef SHADOWGRP
|
|
|
|
const struct sgrp *sg;
|
|
|
|
struct sgrp newsg;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
const struct group *gr;
|
|
|
|
struct group newgr;
|
|
|
|
int errors = 0;
|
|
|
|
int line = 0;
|
|
|
|
int ok;
|
|
|
|
|
|
|
|
Prog = Basename (argv[0]);
|
|
|
|
|
2008-06-10 02:26:03 +05:30
|
|
|
(void) setlocale (LC_ALL, "");
|
|
|
|
(void) bindtextdomain (PACKAGE, LOCALEDIR);
|
|
|
|
(void) textdomain (PACKAGE);
|
2007-12-29 04:24:35 +05:30
|
|
|
|
|
|
|
process_flags(argc, argv);
|
|
|
|
|
|
|
|
check_perms ();
|
|
|
|
|
2008-01-26 23:11:20 +05:30
|
|
|
#ifdef SHADOWGRP
|
2007-12-29 04:24:35 +05:30
|
|
|
is_shadow_grp = sgr_file_present ();
|
2008-01-26 23:11:20 +05:30
|
|
|
#endif
|
2007-12-29 04:24:35 +05:30
|
|
|
|
|
|
|
open_files ();
|
2007-10-07 17:17:22 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* Read each line, separating the group name from the password. The
|
2007-12-29 04:44:59 +05:30
|
|
|
* group entry for each group will be looked up in the appropriate
|
2007-10-07 17:17:22 +05:30
|
|
|
* file (gshadow or group) and the password changed.
|
|
|
|
*/
|
|
|
|
while (fgets (buf, sizeof buf, stdin) != (char *) 0) {
|
|
|
|
line++;
|
2007-12-29 04:44:59 +05:30
|
|
|
cp = strrchr (buf, '\n');
|
|
|
|
if (NULL != cp) {
|
2007-10-07 17:17:22 +05:30
|
|
|
*cp = '\0';
|
|
|
|
} else {
|
|
|
|
fprintf (stderr, _("%s: line %d: line too long\n"),
|
2007-12-29 04:29:17 +05:30
|
|
|
Prog, line);
|
2007-10-07 17:17:22 +05:30
|
|
|
errors++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2007-12-29 04:44:59 +05:30
|
|
|
* The group's name is the first field. It is separated from
|
|
|
|
* the password with a ":" character which is replaced with a
|
2007-10-07 17:17:22 +05:30
|
|
|
* NUL to give the new password. The new password will then
|
|
|
|
* be encrypted in the normal fashion with a new salt
|
|
|
|
* generated, unless the '-e' is given, in which case it is
|
|
|
|
* assumed to already be encrypted.
|
|
|
|
*/
|
|
|
|
|
|
|
|
name = buf;
|
2007-12-29 04:44:59 +05:30
|
|
|
cp = strchr (name, ':');
|
|
|
|
if (NULL != cp) {
|
|
|
|
*cp = '\0';
|
|
|
|
cp++;
|
2007-10-07 17:17:22 +05:30
|
|
|
} else {
|
|
|
|
fprintf (stderr,
|
2007-12-29 04:29:17 +05:30
|
|
|
_("%s: line %d: missing new password\n"),
|
|
|
|
Prog, line);
|
2007-10-07 17:17:22 +05:30
|
|
|
errors++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
newpwd = cp;
|
2007-11-24 01:39:57 +05:30
|
|
|
if (!eflg &&
|
|
|
|
(NULL == crypt_method ||
|
|
|
|
0 != strcmp(crypt_method, "NONE"))) {
|
2007-11-20 15:03:52 +05:30
|
|
|
void *arg = NULL;
|
2007-12-29 04:44:59 +05:30
|
|
|
if (md5flg) {
|
2007-11-20 15:03:52 +05:30
|
|
|
crypt_method = "MD5";
|
2007-12-29 04:44:59 +05:30
|
|
|
} else if (crypt_method != NULL) {
|
|
|
|
if (sflg) {
|
2007-11-20 15:03:52 +05:30
|
|
|
arg = &sha_rounds;
|
2007-12-29 04:44:59 +05:30
|
|
|
}
|
|
|
|
} else {
|
2007-11-24 05:46:41 +05:30
|
|
|
crypt_method = NULL;
|
2007-12-29 04:44:59 +05:30
|
|
|
}
|
2007-11-20 15:03:52 +05:30
|
|
|
cp = pw_encrypt (newpwd,
|
|
|
|
crypt_make_salt(crypt_method, arg));
|
2007-10-07 17:17:22 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2007-12-29 04:44:59 +05:30
|
|
|
* Get the group file entry for this group. The group must
|
2007-10-07 17:17:22 +05:30
|
|
|
* already exist.
|
|
|
|
*/
|
|
|
|
gr = gr_locate (name);
|
2007-12-29 04:44:59 +05:30
|
|
|
if (NULL == gr) {
|
2007-10-07 17:17:22 +05:30
|
|
|
fprintf (stderr,
|
2007-12-29 04:29:17 +05:30
|
|
|
_("%s: line %d: unknown group %s\n"), Prog,
|
|
|
|
line, name);
|
2007-10-07 17:17:22 +05:30
|
|
|
errors++;
|
|
|
|
continue;
|
|
|
|
}
|
2007-10-07 17:17:33 +05:30
|
|
|
#ifdef SHADOWGRP
|
2007-12-29 04:44:59 +05:30
|
|
|
if (is_shadow_grp) {
|
2007-11-21 02:29:42 +05:30
|
|
|
sg = sgr_locate (name);
|
2007-12-29 04:44:59 +05:30
|
|
|
} else {
|
2007-11-21 02:29:42 +05:30
|
|
|
sg = NULL;
|
2007-12-29 04:44:59 +05:30
|
|
|
}
|
2007-10-07 17:17:33 +05:30
|
|
|
#endif
|
2007-10-07 17:17:22 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* The freshly encrypted new password is merged into the
|
2007-12-29 04:44:59 +05:30
|
|
|
* group's entry.
|
2007-10-07 17:17:22 +05:30
|
|
|
*/
|
2007-10-07 17:17:33 +05:30
|
|
|
#ifdef SHADOWGRP
|
2007-12-29 04:44:59 +05:30
|
|
|
if (NULL != sg) {
|
2007-11-21 02:29:42 +05:30
|
|
|
newsg = *sg;
|
|
|
|
newsg.sg_passwd = cp;
|
|
|
|
} else
|
2007-10-07 17:17:33 +05:30
|
|
|
#endif
|
2007-11-21 02:29:42 +05:30
|
|
|
{
|
|
|
|
newgr = *gr;
|
|
|
|
newgr.gr_passwd = cp;
|
|
|
|
}
|
2007-10-07 17:17:22 +05:30
|
|
|
|
|
|
|
/*
|
2007-12-29 04:44:59 +05:30
|
|
|
* The updated group file entry is then put back and will
|
|
|
|
* be written to the group file later, after all the
|
2007-10-07 17:17:22 +05:30
|
|
|
* other entries have been updated as well.
|
|
|
|
*/
|
2007-10-07 17:17:33 +05:30
|
|
|
#ifdef SHADOWGRP
|
2007-12-29 04:44:59 +05:30
|
|
|
if (NULL != sg) {
|
2007-11-21 02:29:42 +05:30
|
|
|
ok = sgr_update (&newsg);
|
2007-12-29 04:44:59 +05:30
|
|
|
} else
|
2007-10-07 17:17:33 +05:30
|
|
|
#endif
|
2007-12-29 04:44:59 +05:30
|
|
|
{
|
2007-11-21 02:29:42 +05:30
|
|
|
ok = gr_update (&newgr);
|
2007-12-29 04:44:59 +05:30
|
|
|
}
|
2007-10-07 17:17:22 +05:30
|
|
|
|
2008-06-10 02:26:03 +05:30
|
|
|
if (0 == ok) {
|
2007-10-07 17:17:22 +05:30
|
|
|
fprintf (stderr,
|
2007-12-29 04:29:17 +05:30
|
|
|
_
|
2007-12-29 04:44:59 +05:30
|
|
|
("%s: line %d: cannot update group entry\n"),
|
2007-12-29 04:29:17 +05:30
|
|
|
Prog, line);
|
2007-10-07 17:17:22 +05:30
|
|
|
errors++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Any detected errors will cause the entire set of changes to be
|
2007-12-29 04:44:59 +05:30
|
|
|
* aborted. Unlocking the group file will cause all of the
|
2007-10-07 17:17:22 +05:30
|
|
|
* changes to be ignored. Otherwise the file is closed, causing the
|
|
|
|
* changes to be written out all at once, and then unlocked
|
|
|
|
* afterwards.
|
|
|
|
*/
|
2008-06-10 02:26:03 +05:30
|
|
|
if (0 != errors) {
|
2007-10-07 17:17:22 +05:30
|
|
|
fprintf (stderr,
|
2007-12-29 04:29:17 +05:30
|
|
|
_("%s: error detected, changes ignored\n"), Prog);
|
2007-10-07 17:17:33 +05:30
|
|
|
#ifdef SHADOWGRP
|
2007-12-29 04:44:59 +05:30
|
|
|
if (is_shadow_grp) {
|
2007-10-07 17:17:22 +05:30
|
|
|
sgr_unlock ();
|
2007-12-29 04:44:59 +05:30
|
|
|
}
|
2007-10-07 17:17:33 +05:30
|
|
|
#endif
|
2007-10-07 17:17:22 +05:30
|
|
|
gr_unlock ();
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
2007-12-29 04:24:35 +05:30
|
|
|
close_files ();
|
2007-10-07 17:17:22 +05:30
|
|
|
|
2007-12-29 04:24:35 +05:30
|
|
|
nscd_flush_cache ("group");
|
2007-10-07 17:17:22 +05:30
|
|
|
|
|
|
|
#ifdef USE_PAM
|
2007-12-29 04:24:35 +05:30
|
|
|
pam_end (pamh, PAM_SUCCESS);
|
2007-10-07 17:17:22 +05:30
|
|
|
#endif /* USE_PAM */
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
2007-12-29 04:29:17 +05:30
|
|
|
|