2007-10-07 17:14:02 +05:30
|
|
|
/*
|
|
|
|
* Copyright 1991 - 1994, 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
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL JULIE HAUGH 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include "rcsid.h"
|
2007-10-07 17:16:07 +05:30
|
|
|
RCSID (PKG_VER "$Id: groupdel.c,v 1.20 2005/04/06 04:26:06 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>
|
|
|
|
#include <pwd.h>
|
2007-10-07 17:14:38 +05:30
|
|
|
#ifdef USE_PAM
|
|
|
|
#include <security/pam_appl.h>
|
|
|
|
#include <security/pam_misc.h>
|
|
|
|
#include <pwd.h>
|
2007-10-07 17:15:23 +05:30
|
|
|
#endif /* USE_PAM */
|
|
|
|
#include "nscd.h"
|
2007-10-07 17:14:02 +05:30
|
|
|
#include "prototypes.h"
|
|
|
|
#include "defines.h"
|
2007-10-07 17:15:23 +05:30
|
|
|
static char *group_name;
|
|
|
|
static char *Prog;
|
|
|
|
static int errors;
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
#include "groupio.h"
|
|
|
|
|
|
|
|
#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_NOTFOUND 6 /* specified group doesn't exist */
|
|
|
|
#define E_GROUP_BUSY 8 /* can't remove user's primary group */
|
|
|
|
#define E_GRP_UPDATE 10 /* can't update group file */
|
|
|
|
|
|
|
|
/* local function prototypes */
|
2007-10-07 17:15:23 +05:30
|
|
|
static void usage (void);
|
|
|
|
static void grp_update (void);
|
|
|
|
static void close_files (void);
|
|
|
|
static void open_files (void);
|
|
|
|
static void group_busy (gid_t);
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* usage - display usage message and exit
|
|
|
|
*/
|
|
|
|
|
2007-10-07 17:15:23 +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: groupdel group\n"));
|
|
|
|
exit (E_USAGE);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* grp_update - update group file entries
|
|
|
|
*
|
|
|
|
* grp_update() writes the new records to the group files.
|
|
|
|
*/
|
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
static void grp_update (void)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:15:23 +05:30
|
|
|
if (!gr_remove (group_name)) {
|
2007-10-07 17:16:07 +05:30
|
|
|
fprintf (stderr, _("%s: error removing group entry\n"), Prog);
|
2007-10-07 17:14:02 +05:30
|
|
|
errors++;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef SHADOWGRP
|
|
|
|
/*
|
|
|
|
* Delete the shadow group entries as well.
|
|
|
|
*/
|
2007-10-07 17:15:23 +05:30
|
|
|
if (is_shadow_grp && !sgr_remove (group_name)) {
|
|
|
|
fprintf (stderr,
|
2007-10-07 17:16:07 +05:30
|
|
|
_("%s: error removing shadow group entry\n"), Prog);
|
2007-10-07 17:14:02 +05:30
|
|
|
errors++;
|
|
|
|
}
|
2007-10-07 17:15:23 +05:30
|
|
|
#endif /* SHADOWGRP */
|
|
|
|
SYSLOG ((LOG_INFO, "remove group `%s'\n", group_name));
|
2007-10-07 17:14:02 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* close_files - close all of the files that were opened
|
|
|
|
*
|
|
|
|
* 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:15:23 +05:30
|
|
|
static void close_files (void)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:15:23 +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:02 +05:30
|
|
|
errors++;
|
|
|
|
}
|
2007-10-07 17:15:23 +05:30
|
|
|
gr_unlock ();
|
2007-10-07 17:14:02 +05:30
|
|
|
#ifdef SHADOWGRP
|
2007-10-07 17:15:23 +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:02 +05:30
|
|
|
errors++;
|
|
|
|
}
|
|
|
|
if (is_shadow_grp)
|
2007-10-07 17:15:23 +05:30
|
|
|
sgr_unlock ();
|
|
|
|
#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:15:23 +05:30
|
|
|
static void open_files (void)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:15:23 +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:15:23 +05:30
|
|
|
exit (E_GRP_UPDATE);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2007-10-07 17:15:23 +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:15:23 +05:30
|
|
|
exit (E_GRP_UPDATE);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
#ifdef SHADOWGRP
|
2007-10-07 17:15:23 +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:15:23 +05:30
|
|
|
exit (E_GRP_UPDATE);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2007-10-07 17:15:23 +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:15:23 +05:30
|
|
|
exit (E_GRP_UPDATE);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2007-10-07 17:15:23 +05:30
|
|
|
#endif /* SHADOWGRP */
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* group_busy - check if this is any user's primary group
|
|
|
|
*
|
|
|
|
* group_busy verifies that this group is not the primary group
|
|
|
|
* for any user. You must remove all users before you remove
|
|
|
|
* the group.
|
|
|
|
*/
|
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
static void group_busy (gid_t gid)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:15:23 +05:30
|
|
|
struct passwd *pwd;
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* Nice slow linear search.
|
|
|
|
*/
|
|
|
|
|
|
|
|
setpwent ();
|
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
while ((pwd = getpwent ()) && pwd->pw_gid != gid);
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
endpwent ();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If pwd isn't NULL, it stopped becaues the gid's matched.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (pwd == (struct passwd *) 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Can't remove the group.
|
|
|
|
*/
|
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
fprintf (stderr, _("%s: cannot remove user's primary group.\n"), Prog);
|
2007-10-07 17:15:23 +05:30
|
|
|
exit (E_GROUP_BUSY);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
2007-10-07 17:14:38 +05:30
|
|
|
#ifdef USE_PAM
|
|
|
|
static struct pam_conv conv = {
|
2007-10-07 17:15:23 +05:30
|
|
|
misc_conv,
|
|
|
|
NULL
|
2007-10-07 17:14:38 +05:30
|
|
|
};
|
2007-10-07 17:15:23 +05:30
|
|
|
#endif /* USE_PAM */
|
2007-10-07 17:14:38 +05:30
|
|
|
|
2007-10-07 17:14:02 +05:30
|
|
|
/*
|
|
|
|
* main - groupdel command
|
|
|
|
*
|
|
|
|
* The syntax of the groupdel command is
|
|
|
|
*
|
|
|
|
* groupdel group
|
|
|
|
*
|
|
|
|
* The named group will be deleted.
|
|
|
|
*/
|
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
int main (int argc, char **argv)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
|
|
|
struct group *grp;
|
2007-10-07 17:15:23 +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:15:23 +05:30
|
|
|
Prog = Basename (argv[0]);
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
setlocale (LC_ALL, "");
|
|
|
|
bindtextdomain (PACKAGE, LOCALEDIR);
|
|
|
|
textdomain (PACKAGE);
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-10-07 17:15:40 +05:30
|
|
|
if (argc != 2)
|
2007-10-07 17:16:07 +05:30
|
|
|
usage ();
|
2007-10-07 17:15:40 +05:30
|
|
|
|
|
|
|
group_name = argv[1];
|
|
|
|
|
2007-10-07 17:14:38 +05:30
|
|
|
#ifdef USE_PAM
|
|
|
|
retval = PAM_SUCCESS;
|
|
|
|
|
2007-10-07 17:15:23 +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 ("groupdel", pampw->pw_name, &conv, &pamh);
|
2007-10-07 17:14:38 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if (retval == PAM_SUCCESS) {
|
2007-10-07 17:15:23 +05:30
|
|
|
retval = pam_authenticate (pamh, 0);
|
2007-10-07 17:14:38 +05:30
|
|
|
if (retval != PAM_SUCCESS) {
|
2007-10-07 17:15:23 +05:30
|
|
|
pam_end (pamh, retval);
|
2007-10-07 17:14:38 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (retval == PAM_SUCCESS) {
|
2007-10-07 17:15:23 +05:30
|
|
|
retval = pam_acct_mgmt (pamh, 0);
|
2007-10-07 17:14:38 +05:30
|
|
|
if (retval != PAM_SUCCESS) {
|
2007-10-07 17:15:23 +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:23 +05:30
|
|
|
#endif /* USE_PAM */
|
2007-10-07 17:14:38 +05:30
|
|
|
|
2007-10-07 17:15:40 +05:30
|
|
|
OPENLOG ("groupdel");
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
#ifdef SHADOWGRP
|
2007-10-07 17:15:23 +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:15:23 +05:30
|
|
|
if (!(grp = getgrnam (group_name))) {
|
|
|
|
fprintf (stderr, _("%s: group %s does not exist\n"),
|
|
|
|
Prog, group_name);
|
|
|
|
exit (E_NOTFOUND);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
#ifdef USE_NIS
|
2007-10-07 17:14:02 +05:30
|
|
|
/*
|
|
|
|
* Make sure this isn't a NIS group
|
|
|
|
*/
|
|
|
|
if (__isgrNIS ()) {
|
2007-10-07 17:15:23 +05:30
|
|
|
char *nis_domain;
|
|
|
|
char *nis_master;
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
fprintf (stderr, _("%s: group %s is a NIS group\n"),
|
|
|
|
Prog, group_name);
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
if (!yp_get_default_domain (&nis_domain) &&
|
|
|
|
!yp_master (nis_domain, "group.byname", &nis_master)) {
|
2007-10-07 17:14:02 +05:30
|
|
|
fprintf (stderr, _("%s: %s is the NIS master\n"),
|
2007-10-07 17:15:23 +05:30
|
|
|
Prog, nis_master);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2007-10-07 17:15:23 +05:30
|
|
|
exit (E_NOTFOUND);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now check to insure that this isn't the primary group of
|
|
|
|
* anyone.
|
|
|
|
*/
|
|
|
|
group_busy (grp->gr_gid);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Do the hard stuff - open the files, delete the group entries,
|
|
|
|
* then close and update the files.
|
|
|
|
*/
|
|
|
|
open_files ();
|
|
|
|
|
|
|
|
grp_update ();
|
2007-10-07 17:15:23 +05:30
|
|
|
nscd_flush_cache ("group");
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
close_files ();
|
2007-10-07 17:14:38 +05:30
|
|
|
|
|
|
|
#ifdef USE_PAM
|
|
|
|
if (retval == PAM_SUCCESS) {
|
2007-10-07 17:15:23 +05:30
|
|
|
retval = pam_chauthtok (pamh, 0);
|
2007-10-07 17:14:38 +05:30
|
|
|
if (retval != PAM_SUCCESS) {
|
2007-10-07 17:15:23 +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:15:23 +05:30
|
|
|
pam_end (pamh, PAM_SUCCESS);
|
|
|
|
#endif /* USE_PAM */
|
|
|
|
exit (errors == 0 ? E_SUCCESS : E_GRP_UPDATE);
|
|
|
|
/* NOT REACHED */
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|