2007-10-07 17:14:02 +05:30
|
|
|
/*
|
|
|
|
* Copyright 1991 - 1993, Julianne Frances Haugh
|
|
|
|
* 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.
|
|
|
|
* 3. Neither the name of Julianne F. Haugh nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY JULIE HAUGH 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
|
2007-10-07 17:14:59 +05:30
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL JULIE HAUGH OR CONTRIBUTORS BE LIABLE
|
2007-10-07 17:14:02 +05:30
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include "rcsid.h"
|
2007-10-07 17:16:34 +05:30
|
|
|
RCSID (PKG_VER "$Id: groupadd.c,v 1.37 2005/07/18 13:17:45 kloczek Exp $")
|
2007-10-07 17:14:02 +05:30
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <grp.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <fcntl.h>
|
2007-10-07 17:16:34 +05:30
|
|
|
#ifdef USE_PAM
|
|
|
|
#include <security/pam_appl.h>
|
|
|
|
#include <security/pam_misc.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#endif /* USE_PAM */
|
2007-10-07 17:14:02 +05:30
|
|
|
#include "defines.h"
|
|
|
|
#include "prototypes.h"
|
|
|
|
#include "chkname.h"
|
|
|
|
#include "getdef.h"
|
|
|
|
#include "groupio.h"
|
2007-10-07 17:15:23 +05:30
|
|
|
#include "nscd.h"
|
2007-10-07 17:14:02 +05:30
|
|
|
#ifdef SHADOWGRP
|
|
|
|
#include "sgroupio.h"
|
|
|
|
static int is_shadow_grp;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* exit status values
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define E_SUCCESS 0 /* success */
|
|
|
|
#define E_USAGE 2 /* invalid command syntax */
|
|
|
|
#define E_BAD_ARG 3 /* invalid argument to option */
|
|
|
|
#define E_GID_IN_USE 4 /* gid not unique (when -o not used) */
|
2007-10-07 17:15:23 +05:30
|
|
|
#define E_NAME_IN_USE 9 /* group name not unique */
|
2007-10-07 17:14:02 +05:30
|
|
|
#define E_GRP_UPDATE 10 /* can't update group file */
|
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
static char *group_name;
|
|
|
|
static gid_t group_id;
|
2007-10-07 17:14:02 +05:30
|
|
|
static char *empty_list = NULL;
|
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
static char *Prog;
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
static int oflg = 0; /* permit non-unique group ID to be specified with -g */
|
|
|
|
static int gflg = 0; /* ID value for the new group */
|
|
|
|
static int fflg = 0; /* if group already exists, do nothing and exit(0) */
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/* local function prototypes */
|
2007-10-07 17:14:59 +05:30
|
|
|
static void usage (void);
|
|
|
|
static void new_grent (struct group *);
|
|
|
|
|
2007-10-07 17:14:02 +05:30
|
|
|
#ifdef SHADOWGRP
|
2007-10-07 17:14:59 +05:30
|
|
|
static void new_sgent (struct sgrp *);
|
2007-10-07 17:14:02 +05:30
|
|
|
#endif
|
2007-10-07 17:14:59 +05:30
|
|
|
static void grp_update (void);
|
|
|
|
static void find_new_gid (void);
|
|
|
|
static void check_new_name (void);
|
|
|
|
static void process_flags (int, char **);
|
|
|
|
static void close_files (void);
|
|
|
|
static void open_files (void);
|
|
|
|
static void fail_exit (int);
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* usage - display usage message and exit
|
|
|
|
*/
|
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
static void usage (void)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:15:23 +05:30
|
|
|
fprintf (stderr, _("Usage: groupadd [-g gid [-o]] [-f] group\n"));
|
2007-10-07 17:14:59 +05:30
|
|
|
exit (E_USAGE);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* new_grent - initialize the values in a group file entry
|
|
|
|
*
|
2007-10-07 17:14:59 +05:30
|
|
|
* new_grent() takes all of the values that have been entered and fills
|
|
|
|
* in a (struct group) with them.
|
2007-10-07 17:14:02 +05:30
|
|
|
*/
|
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
static void new_grent (struct group *grent)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:14:59 +05:30
|
|
|
memzero (grent, sizeof *grent);
|
2007-10-07 17:14:02 +05:30
|
|
|
grent->gr_name = group_name;
|
2007-10-07 17:14:59 +05:30
|
|
|
grent->gr_passwd = SHADOW_PASSWD_STRING; /* XXX warning: const */
|
2007-10-07 17:14:02 +05:30
|
|
|
grent->gr_gid = group_id;
|
|
|
|
grent->gr_mem = &empty_list;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef SHADOWGRP
|
|
|
|
/*
|
|
|
|
* new_sgent - initialize the values in a shadow group file entry
|
|
|
|
*
|
2007-10-07 17:14:59 +05:30
|
|
|
* new_sgent() takes all of the values that have been entered and fills
|
|
|
|
* in a (struct sgrp) with them.
|
2007-10-07 17:14:02 +05:30
|
|
|
*/
|
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
static void new_sgent (struct sgrp *sgent)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:14:59 +05:30
|
|
|
memzero (sgent, sizeof *sgent);
|
2007-10-07 17:14:02 +05:30
|
|
|
sgent->sg_name = group_name;
|
2007-10-07 17:14:59 +05:30
|
|
|
sgent->sg_passwd = "!"; /* XXX warning: const */
|
2007-10-07 17:14:02 +05:30
|
|
|
sgent->sg_adm = &empty_list;
|
|
|
|
sgent->sg_mem = &empty_list;
|
|
|
|
}
|
2007-10-07 17:14:59 +05:30
|
|
|
#endif /* SHADOWGRP */
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* grp_update - add new group file entries
|
|
|
|
*
|
|
|
|
* grp_update() writes the new records to the group files.
|
|
|
|
*/
|
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
static void grp_update (void)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:14:59 +05:30
|
|
|
struct group grp;
|
|
|
|
|
2007-10-07 17:14:02 +05:30
|
|
|
#ifdef SHADOWGRP
|
2007-10-07 17:14:59 +05:30
|
|
|
struct sgrp sgrp;
|
|
|
|
#endif /* SHADOWGRP */
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* Create the initial entries for this new group.
|
|
|
|
*/
|
|
|
|
new_grent (&grp);
|
|
|
|
#ifdef SHADOWGRP
|
|
|
|
new_sgent (&sgrp);
|
2007-10-07 17:14:59 +05:30
|
|
|
#endif /* SHADOWGRP */
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* Write out the new group file entry.
|
|
|
|
*/
|
2007-10-07 17:14:59 +05:30
|
|
|
if (!gr_update (&grp)) {
|
2007-10-07 17:16:07 +05:30
|
|
|
fprintf (stderr, _("%s: error adding new group entry\n"), Prog);
|
2007-10-07 17:14:59 +05:30
|
|
|
fail_exit (E_GRP_UPDATE);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
#ifdef SHADOWGRP
|
|
|
|
/*
|
|
|
|
* Write out the new shadow group entries as well.
|
|
|
|
*/
|
2007-10-07 17:14:59 +05:30
|
|
|
if (is_shadow_grp && !sgr_update (&sgrp)) {
|
2007-10-07 17:16:07 +05:30
|
|
|
fprintf (stderr, _("%s: error adding new group entry\n"), Prog);
|
2007-10-07 17:14:59 +05:30
|
|
|
fail_exit (E_GRP_UPDATE);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2007-10-07 17:14:59 +05:30
|
|
|
#endif /* SHADOWGRP */
|
2007-10-07 17:16:25 +05:30
|
|
|
SYSLOG ((LOG_INFO, "new group: name=%s, GID=%u",
|
2007-10-07 17:15:23 +05:30
|
|
|
group_name, (unsigned int) group_id));
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* find_new_gid - find the next available GID
|
|
|
|
*
|
|
|
|
* find_new_gid() locates the next highest unused GID in the group
|
|
|
|
* file, or checks the given group ID against the existing ones for
|
|
|
|
* uniqueness.
|
|
|
|
*/
|
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
static void find_new_gid (void)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
|
|
|
const struct group *grp;
|
|
|
|
gid_t gid_min, gid_max;
|
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
gid_min = getdef_unum ("GID_MIN", 100);
|
|
|
|
gid_max = getdef_unum ("GID_MAX", 60000);
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* Start with some GID value if the user didn't provide us with
|
|
|
|
* one already.
|
|
|
|
*/
|
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
if (!gflg)
|
2007-10-07 17:14:02 +05:30
|
|
|
group_id = gid_min;
|
|
|
|
|
|
|
|
/*
|
2007-10-07 17:14:59 +05:30
|
|
|
* Search the entire group file, either looking for this GID (if the
|
|
|
|
* user specified one with -g) or looking for the largest unused
|
|
|
|
* value.
|
2007-10-07 17:14:02 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef NO_GETGRENT
|
2007-10-07 17:14:59 +05:30
|
|
|
gr_rewind ();
|
|
|
|
while ((grp = gr_next ())) {
|
2007-10-07 17:14:02 +05:30
|
|
|
#else
|
2007-10-07 17:14:59 +05:30
|
|
|
setgrent ();
|
|
|
|
while ((grp = getgrent ())) {
|
2007-10-07 17:14:02 +05:30
|
|
|
#endif
|
2007-10-07 17:14:59 +05:30
|
|
|
if (strcmp (group_name, grp->gr_name) == 0) {
|
2007-10-07 17:14:02 +05:30
|
|
|
if (fflg) {
|
2007-10-07 17:14:59 +05:30
|
|
|
fail_exit (E_SUCCESS);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2007-10-07 17:14:59 +05:30
|
|
|
fprintf (stderr, _("%s: name %s is not unique\n"),
|
|
|
|
Prog, group_name);
|
|
|
|
fail_exit (E_NAME_IN_USE);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
if (gflg && group_id == grp->gr_gid) {
|
|
|
|
if (fflg) {
|
|
|
|
/* turn off -g and search again */
|
|
|
|
gflg = 0;
|
|
|
|
#ifdef NO_GETGRENT
|
2007-10-07 17:14:59 +05:30
|
|
|
gr_rewind ();
|
2007-10-07 17:14:02 +05:30
|
|
|
#else
|
2007-10-07 17:14:59 +05:30
|
|
|
setgrent ();
|
2007-10-07 17:14:02 +05:30
|
|
|
#endif
|
|
|
|
continue;
|
|
|
|
}
|
2007-10-07 17:16:25 +05:30
|
|
|
fprintf (stderr, _("%s: GID %u is not unique\n"),
|
2007-10-07 17:14:59 +05:30
|
|
|
Prog, (unsigned int) group_id);
|
|
|
|
fail_exit (E_GID_IN_USE);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2007-10-07 17:14:59 +05:30
|
|
|
if (!gflg && grp->gr_gid >= group_id) {
|
2007-10-07 17:14:02 +05:30
|
|
|
if (grp->gr_gid > gid_max)
|
|
|
|
continue;
|
|
|
|
group_id = grp->gr_gid + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!gflg && group_id == gid_max + 1) {
|
|
|
|
for (group_id = gid_min; group_id < gid_max; group_id++) {
|
|
|
|
#ifdef NO_GETGRENT
|
2007-10-07 17:14:59 +05:30
|
|
|
gr_rewind ();
|
|
|
|
while ((grp = gr_next ())
|
|
|
|
&& grp->gr_gid != group_id);
|
2007-10-07 17:14:02 +05:30
|
|
|
if (!grp)
|
|
|
|
break;
|
|
|
|
#else
|
2007-10-07 17:14:59 +05:30
|
|
|
if (!getgrgid (group_id))
|
2007-10-07 17:14:02 +05:30
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
if (group_id == gid_max) {
|
2007-10-07 17:16:25 +05:30
|
|
|
fprintf (stderr, _("%s: can't get unique GID\n"), Prog);
|
2007-10-07 17:14:59 +05:30
|
|
|
fail_exit (E_GID_IN_USE);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* check_new_name - check the new name for validity
|
|
|
|
*
|
2007-10-07 17:14:59 +05:30
|
|
|
* check_new_name() insures that the new name doesn't contain any
|
|
|
|
* illegal characters.
|
2007-10-07 17:14:02 +05:30
|
|
|
*/
|
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
static void check_new_name (void)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:14:59 +05:30
|
|
|
if (check_group_name (group_name))
|
2007-10-07 17:14:02 +05:30
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* All invalid group names land here.
|
|
|
|
*/
|
|
|
|
|
2007-10-07 17:15:40 +05:30
|
|
|
fprintf (stderr, _("%s: %s is not a valid group name\n"),
|
2007-10-07 17:14:59 +05:30
|
|
|
Prog, group_name);
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
exit (E_BAD_ARG);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* process_flags - perform command line argument setting
|
|
|
|
*
|
2007-10-07 17:14:59 +05:30
|
|
|
* process_flags() interprets the command line arguments and sets the
|
|
|
|
* values that the user will be created with accordingly. The values
|
|
|
|
* are checked for sanity.
|
2007-10-07 17:14:02 +05:30
|
|
|
*/
|
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
static void process_flags (int argc, char **argv)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
|
|
|
char *cp;
|
|
|
|
int arg;
|
|
|
|
|
2007-10-07 17:16:34 +05:30
|
|
|
while ((arg = getopt (argc, argv, "fg:K:o")) != EOF) {
|
2007-10-07 17:14:02 +05:30
|
|
|
switch (arg) {
|
2007-10-07 17:16:34 +05:30
|
|
|
case 'f':
|
|
|
|
/*
|
|
|
|
* "force" - do nothing, just exit(0), if the
|
|
|
|
* specified group already exists. With -g, if
|
|
|
|
* specified gid already exists, choose another
|
|
|
|
* (unique) gid (turn off -g). Based on the RedHat's
|
|
|
|
* patch from shadow-utils-970616-9.
|
|
|
|
*/
|
|
|
|
fflg++;
|
|
|
|
break;
|
2007-10-07 17:14:02 +05:30
|
|
|
case 'g':
|
|
|
|
gflg++;
|
2007-10-07 17:14:59 +05:30
|
|
|
if (!isdigit (optarg[0]))
|
2007-10-07 17:14:02 +05:30
|
|
|
usage ();
|
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
group_id = strtoul (optarg, &cp, 10);
|
2007-10-07 17:14:02 +05:30
|
|
|
if (*cp != '\0') {
|
2007-10-07 17:14:59 +05:30
|
|
|
fprintf (stderr,
|
|
|
|
_("%s: invalid group %s\n"), Prog,
|
|
|
|
optarg);
|
|
|
|
fail_exit (E_BAD_ARG);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
break;
|
2007-10-07 17:16:34 +05:30
|
|
|
case 'K':
|
2007-10-07 17:14:02 +05:30
|
|
|
/*
|
2007-10-07 17:16:34 +05:30
|
|
|
* override login.defs defaults (-K name=value)
|
|
|
|
* example: -K GID_MIN=100 -K GID_MAX=499
|
|
|
|
* note: -K GID_MIN=10,GID_MAX=499 doesn't work yet
|
2007-10-07 17:14:02 +05:30
|
|
|
*/
|
2007-10-07 17:14:59 +05:30
|
|
|
cp = strchr (optarg, '=');
|
2007-10-07 17:14:02 +05:30
|
|
|
if (!cp) {
|
2007-10-07 17:14:59 +05:30
|
|
|
fprintf (stderr,
|
2007-10-07 17:16:34 +05:30
|
|
|
_("%s: -K requires KEY=VALUE\n"),
|
2007-10-07 17:14:59 +05:30
|
|
|
Prog);
|
|
|
|
exit (E_BAD_ARG);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
/* terminate name, point to value */
|
|
|
|
*cp++ = '\0';
|
2007-10-07 17:14:59 +05:30
|
|
|
if (putdef_str (optarg, cp) < 0)
|
|
|
|
exit (E_BAD_ARG);
|
2007-10-07 17:14:02 +05:30
|
|
|
break;
|
2007-10-07 17:16:34 +05:30
|
|
|
case 'o':
|
|
|
|
oflg++;
|
2007-10-07 17:14:02 +05:30
|
|
|
break;
|
|
|
|
default:
|
2007-10-07 17:14:59 +05:30
|
|
|
usage ();
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (oflg && !gflg)
|
2007-10-07 17:14:59 +05:30
|
|
|
usage ();
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
if (optind != argc - 1)
|
2007-10-07 17:14:59 +05:30
|
|
|
usage ();
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
group_name = argv[argc - 1];
|
2007-10-07 17:14:59 +05:30
|
|
|
check_new_name ();
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* close_files - close all of the files that were opened
|
|
|
|
*
|
2007-10-07 17:14:59 +05:30
|
|
|
* close_files() closes all of the files that were opened for this new
|
|
|
|
* group. This causes any modified entries to be written out.
|
2007-10-07 17:14:02 +05:30
|
|
|
*/
|
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
static void close_files (void)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:14:59 +05:30
|
|
|
if (!gr_close ()) {
|
2007-10-07 17:16:07 +05:30
|
|
|
fprintf (stderr, _("%s: cannot rewrite group file\n"), Prog);
|
2007-10-07 17:14:59 +05:30
|
|
|
fail_exit (E_GRP_UPDATE);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2007-10-07 17:14:59 +05:30
|
|
|
gr_unlock ();
|
2007-10-07 17:14:02 +05:30
|
|
|
#ifdef SHADOWGRP
|
2007-10-07 17:14:59 +05:30
|
|
|
if (is_shadow_grp && !sgr_close ()) {
|
|
|
|
fprintf (stderr,
|
2007-10-07 17:16:07 +05:30
|
|
|
_("%s: cannot rewrite shadow group file\n"), Prog);
|
2007-10-07 17:14:59 +05:30
|
|
|
fail_exit (E_GRP_UPDATE);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
if (is_shadow_grp)
|
|
|
|
sgr_unlock ();
|
2007-10-07 17:14:59 +05:30
|
|
|
#endif /* SHADOWGRP */
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* open_files - lock and open the group files
|
|
|
|
*
|
|
|
|
* open_files() opens the two group files.
|
|
|
|
*/
|
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
static void open_files (void)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:14:59 +05:30
|
|
|
if (!gr_lock ()) {
|
2007-10-07 17:16:07 +05:30
|
|
|
fprintf (stderr, _("%s: unable to lock group file\n"), Prog);
|
2007-10-07 17:14:59 +05:30
|
|
|
exit (E_GRP_UPDATE);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2007-10-07 17:14:59 +05:30
|
|
|
if (!gr_open (O_RDWR)) {
|
2007-10-07 17:16:07 +05:30
|
|
|
fprintf (stderr, _("%s: unable to open group file\n"), Prog);
|
2007-10-07 17:14:59 +05:30
|
|
|
fail_exit (E_GRP_UPDATE);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
#ifdef SHADOWGRP
|
2007-10-07 17:14:59 +05:30
|
|
|
if (is_shadow_grp && !sgr_lock ()) {
|
|
|
|
fprintf (stderr,
|
2007-10-07 17:16:07 +05:30
|
|
|
_("%s: unable to lock shadow group file\n"), Prog);
|
2007-10-07 17:14:59 +05:30
|
|
|
fail_exit (E_GRP_UPDATE);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2007-10-07 17:14:59 +05:30
|
|
|
if (is_shadow_grp && !sgr_open (O_RDWR)) {
|
|
|
|
fprintf (stderr,
|
2007-10-07 17:16:07 +05:30
|
|
|
_("%s: unable to open shadow group file\n"), Prog);
|
2007-10-07 17:14:59 +05:30
|
|
|
fail_exit (E_GRP_UPDATE);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2007-10-07 17:14:59 +05:30
|
|
|
#endif /* SHADOWGRP */
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* fail_exit - exit with an error code after unlocking files
|
|
|
|
*/
|
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
static void fail_exit (int code)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
|
|
|
(void) gr_unlock ();
|
|
|
|
#ifdef SHADOWGRP
|
|
|
|
if (is_shadow_grp)
|
|
|
|
sgr_unlock ();
|
|
|
|
#endif
|
|
|
|
exit (code);
|
|
|
|
}
|
|
|
|
|
2007-10-07 17:14:38 +05:30
|
|
|
#ifdef USE_PAM
|
|
|
|
static struct pam_conv conv = {
|
2007-10-07 17:14:59 +05:30
|
|
|
misc_conv,
|
|
|
|
NULL
|
2007-10-07 17:14:38 +05:30
|
|
|
};
|
2007-10-07 17:14:59 +05:30
|
|
|
#endif /* USE_PAM */
|
2007-10-07 17:14:38 +05:30
|
|
|
|
2007-10-07 17:14:02 +05:30
|
|
|
/*
|
|
|
|
* main - groupadd command
|
|
|
|
*/
|
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
int main (int argc, char **argv)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:14:38 +05:30
|
|
|
#ifdef USE_PAM
|
|
|
|
pam_handle_t *pamh = NULL;
|
|
|
|
struct passwd *pampw;
|
|
|
|
int retval;
|
|
|
|
#endif
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* Get my name so that I can use it to report errors.
|
|
|
|
*/
|
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
Prog = Basename (argv[0]);
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
setlocale (LC_ALL, "");
|
|
|
|
bindtextdomain (PACKAGE, LOCALEDIR);
|
|
|
|
textdomain (PACKAGE);
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
OPENLOG ("groupadd");
|
2007-10-07 17:15:40 +05:30
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
process_flags (argc, argv);
|
2007-10-07 17:15:40 +05:30
|
|
|
|
2007-10-07 17:14:38 +05:30
|
|
|
#ifdef USE_PAM
|
|
|
|
retval = PAM_SUCCESS;
|
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
pampw = getpwuid (getuid ());
|
2007-10-07 17:14:38 +05:30
|
|
|
if (pampw == NULL) {
|
|
|
|
retval = PAM_USER_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (retval == PAM_SUCCESS) {
|
2007-10-07 17:16:07 +05:30
|
|
|
retval = pam_start ("groupadd", pampw->pw_name, &conv, &pamh);
|
2007-10-07 17:14:38 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if (retval == PAM_SUCCESS) {
|
2007-10-07 17:14:59 +05:30
|
|
|
retval = pam_authenticate (pamh, 0);
|
2007-10-07 17:14:38 +05:30
|
|
|
if (retval != PAM_SUCCESS) {
|
2007-10-07 17:14:59 +05:30
|
|
|
pam_end (pamh, retval);
|
2007-10-07 17:14:38 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (retval == PAM_SUCCESS) {
|
2007-10-07 17:14:59 +05:30
|
|
|
retval = pam_acct_mgmt (pamh, 0);
|
2007-10-07 17:14:38 +05:30
|
|
|
if (retval != PAM_SUCCESS) {
|
2007-10-07 17:14:59 +05:30
|
|
|
pam_end (pamh, retval);
|
2007-10-07 17:14:38 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (retval != PAM_SUCCESS) {
|
2007-10-07 17:16:07 +05:30
|
|
|
fprintf (stderr, _("%s: PAM authentication failed\n"), Prog);
|
2007-10-07 17:14:38 +05:30
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
2007-10-07 17:15:40 +05:30
|
|
|
OPENLOG ("groupadd");
|
|
|
|
#endif /* USE_PAM */
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
#ifdef SHADOWGRP
|
2007-10-07 17:14:59 +05:30
|
|
|
is_shadow_grp = sgr_file_present ();
|
2007-10-07 17:14:02 +05:30
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Start with a quick check to see if the group exists.
|
|
|
|
*/
|
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
if (getgrnam (group_name)) {
|
2007-10-07 17:14:02 +05:30
|
|
|
if (fflg) {
|
2007-10-07 17:14:59 +05:30
|
|
|
exit (E_SUCCESS);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2007-10-07 17:16:07 +05:30
|
|
|
fprintf (stderr, _("%s: group %s exists\n"), Prog, group_name);
|
2007-10-07 17:14:59 +05:30
|
|
|
exit (E_NAME_IN_USE);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Do the hard stuff - open the files, create the group entries,
|
|
|
|
* then close and update the files.
|
|
|
|
*/
|
2007-10-07 17:14:59 +05:30
|
|
|
open_files ();
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
if (!gflg || !oflg)
|
2007-10-07 17:14:59 +05:30
|
|
|
find_new_gid ();
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
grp_update ();
|
2007-10-07 17:15:23 +05:30
|
|
|
nscd_flush_cache ("group");
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
close_files ();
|
2007-10-07 17:14:38 +05:30
|
|
|
|
|
|
|
#ifdef USE_PAM
|
|
|
|
if (retval == PAM_SUCCESS) {
|
2007-10-07 17:14:59 +05:30
|
|
|
retval = pam_chauthtok (pamh, 0);
|
2007-10-07 17:14:38 +05:30
|
|
|
if (retval != PAM_SUCCESS) {
|
2007-10-07 17:14:59 +05:30
|
|
|
pam_end (pamh, retval);
|
2007-10-07 17:14:38 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (retval != PAM_SUCCESS) {
|
|
|
|
fprintf (stderr, _("%s: PAM chauthtok failed\n"), Prog);
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (retval == PAM_SUCCESS)
|
2007-10-07 17:14:59 +05:30
|
|
|
pam_end (pamh, PAM_SUCCESS);
|
|
|
|
#endif /* USE_PAM */
|
|
|
|
exit (E_SUCCESS);
|
2007-10-07 17:15:23 +05:30
|
|
|
/* NOT REACHED */
|
|
|
|
}
|