2007-10-07 17:14:02 +05:30
|
|
|
/*
|
|
|
|
* Copyright 1992 - 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
|
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>
|
|
|
|
|
2007-11-11 05:16:11 +05:30
|
|
|
#ident "$Id$"
|
2007-10-07 17:17:01 +05:30
|
|
|
|
2007-10-07 17:14:02 +05:30
|
|
|
#include <fcntl.h>
|
|
|
|
#include <grp.h>
|
|
|
|
#include <pwd.h>
|
2007-10-07 17:17:01 +05:30
|
|
|
#include <stdio.h>
|
|
|
|
#include "chkname.h"
|
2007-10-07 17:14:02 +05:30
|
|
|
#include "commonio.h"
|
2007-10-07 17:17:01 +05:30
|
|
|
#include "defines.h"
|
2007-10-07 17:14:02 +05:30
|
|
|
#include "groupio.h"
|
2007-10-07 17:17:22 +05:30
|
|
|
#include "nscd.h"
|
2007-10-07 17:17:01 +05:30
|
|
|
#include "prototypes.h"
|
2007-10-07 17:14:59 +05:30
|
|
|
extern void __gr_del_entry (const struct commonio_entry *);
|
|
|
|
extern struct commonio_entry *__gr_get_head (void);
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
#ifdef SHADOWGRP
|
|
|
|
#include "sgroupio.h"
|
2007-10-07 17:14:59 +05:30
|
|
|
extern void __sgr_del_entry (const struct commonio_entry *);
|
|
|
|
extern struct commonio_entry *__sgr_get_head (void);
|
2007-10-07 17:14:02 +05:30
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Exit codes
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define E_OKAY 0
|
|
|
|
#define E_USAGE 1
|
|
|
|
#define E_BAD_ENTRY 2
|
|
|
|
#define E_CANT_OPEN 3
|
|
|
|
#define E_CANT_LOCK 4
|
|
|
|
#define E_CANT_UPDATE 5
|
|
|
|
|
|
|
|
/*
|
2007-10-07 17:17:01 +05:30
|
|
|
* Global variables
|
2007-10-07 17:14:02 +05:30
|
|
|
*/
|
|
|
|
static char *Prog;
|
|
|
|
static const char *grp_file = GROUP_FILE;
|
2007-10-07 17:14:59 +05:30
|
|
|
|
2007-10-07 17:14:02 +05:30
|
|
|
#ifdef SHADOWGRP
|
|
|
|
static const char *sgr_file = SGROUP_FILE;
|
|
|
|
#endif
|
|
|
|
static int read_only = 0;
|
|
|
|
|
|
|
|
/* local function prototypes */
|
2007-10-07 17:14:59 +05:30
|
|
|
static void usage (void);
|
|
|
|
static int yes_or_no (void);
|
|
|
|
static void delete_member (char **, const char *);
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* usage - print syntax message and exit
|
|
|
|
*/
|
2007-10-07 17:14:59 +05:30
|
|
|
static void usage (void)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
|
|
|
#ifdef SHADOWGRP
|
2007-10-07 17:16:07 +05:30
|
|
|
fprintf (stderr, _("Usage: %s [-r] [-s] [group [gshadow]]\n"), Prog);
|
2007-10-07 17:14:02 +05:30
|
|
|
#else
|
2007-10-07 17:14:59 +05:30
|
|
|
fprintf (stderr, _("Usage: %s [-r] [-s] [group]\n"), Prog);
|
2007-10-07 17:14:02 +05:30
|
|
|
#endif
|
2007-10-07 17:14:59 +05:30
|
|
|
exit (E_USAGE);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* yes_or_no - get answer to question from the user
|
|
|
|
*/
|
2007-10-07 17:14:59 +05:30
|
|
|
static int yes_or_no (void)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:14:59 +05:30
|
|
|
char buf[80];
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* In read-only mode all questions are answered "no".
|
|
|
|
*/
|
|
|
|
if (read_only) {
|
2007-10-07 17:16:52 +05:30
|
|
|
printf (_("No\n"));
|
2007-10-07 17:14:02 +05:30
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get a line and see what the first character is.
|
|
|
|
*/
|
2007-10-07 17:14:59 +05:30
|
|
|
if (fgets (buf, sizeof buf, stdin))
|
2007-10-07 17:14:02 +05:30
|
|
|
return buf[0] == 'y' || buf[0] == 'Y';
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* delete_member - delete an entry in a list of members
|
|
|
|
*/
|
2007-10-07 17:14:59 +05:30
|
|
|
static void delete_member (char **list, const char *member)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; list[i]; i++)
|
|
|
|
if (list[i] == member)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (list[i])
|
|
|
|
for (; list[i]; i++)
|
|
|
|
list[i] = list[i + 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* grpck - verify group file integrity
|
|
|
|
*/
|
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:59 +05:30
|
|
|
int arg;
|
|
|
|
int errors = 0;
|
2007-10-07 17:17:33 +05:30
|
|
|
int changed = 0;
|
2007-10-07 17:14:59 +05:30
|
|
|
int i;
|
|
|
|
struct commonio_entry *gre, *tgre;
|
|
|
|
struct group *grp;
|
|
|
|
int sort_mode = 0;
|
|
|
|
|
2007-10-07 17:14:02 +05:30
|
|
|
#ifdef SHADOWGRP
|
2007-10-07 17:14:59 +05:30
|
|
|
struct commonio_entry *sge, *tsge;
|
|
|
|
struct sgrp *sgr;
|
2007-10-07 17:14:02 +05:30
|
|
|
int is_shadow = 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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:15:40 +05:30
|
|
|
OPENLOG ("grpck");
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse the command line arguments
|
|
|
|
*/
|
2007-10-07 17:14:59 +05:30
|
|
|
while ((arg = getopt (argc, argv, "qrs")) != EOF) {
|
2007-10-07 17:14:02 +05:30
|
|
|
switch (arg) {
|
|
|
|
case 'q':
|
|
|
|
/* quiet - ignored for now */
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
read_only = 1;
|
|
|
|
break;
|
2007-10-07 17:14:51 +05:30
|
|
|
case 's':
|
|
|
|
sort_mode = 1;
|
|
|
|
break;
|
2007-10-07 17:14:02 +05:30
|
|
|
default:
|
2007-10-07 17:14:59 +05:30
|
|
|
usage ();
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-07 17:14:51 +05:30
|
|
|
if (sort_mode && read_only) {
|
2007-10-07 17:16:07 +05:30
|
|
|
fprintf (stderr, _("%s: -s and -r are incompatibile\n"), Prog);
|
2007-10-07 17:14:59 +05:30
|
|
|
exit (E_USAGE);
|
2007-10-07 17:14:51 +05:30
|
|
|
}
|
2007-10-07 17:14:59 +05:30
|
|
|
|
2007-10-07 17:14:02 +05:30
|
|
|
/*
|
|
|
|
* Make certain we have the right number of arguments
|
|
|
|
*/
|
|
|
|
#ifdef SHADOWGRP
|
|
|
|
if (optind != argc && optind + 1 != argc && optind + 2 != argc)
|
|
|
|
#else
|
|
|
|
if (optind != argc && optind + 1 != argc)
|
|
|
|
#endif
|
2007-10-07 17:14:59 +05:30
|
|
|
usage ();
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
2007-10-07 17:14:59 +05:30
|
|
|
* If there are two left over filenames, use those as the group and
|
|
|
|
* group password filenames.
|
2007-10-07 17:14:02 +05:30
|
|
|
*/
|
|
|
|
if (optind != argc) {
|
|
|
|
grp_file = argv[optind];
|
2007-10-07 17:14:59 +05:30
|
|
|
gr_name (grp_file);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
#ifdef SHADOWGRP
|
|
|
|
if (optind + 2 == argc) {
|
|
|
|
sgr_file = argv[optind + 1];
|
2007-10-07 17:14:59 +05:30
|
|
|
sgr_name (sgr_file);
|
2007-10-07 17:14:02 +05:30
|
|
|
is_shadow = 1;
|
|
|
|
} else if (optind == argc)
|
2007-10-07 17:14:59 +05:30
|
|
|
is_shadow = sgr_file_present ();
|
2007-10-07 17:14:02 +05:30
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Lock the files if we aren't in "read-only" mode
|
|
|
|
*/
|
|
|
|
if (!read_only) {
|
2007-10-07 17:14:59 +05:30
|
|
|
if (!gr_lock ()) {
|
|
|
|
fprintf (stderr, _("%s: cannot lock file %s\n"),
|
|
|
|
Prog, grp_file);
|
2007-10-07 17:14:02 +05:30
|
|
|
if (optind == argc)
|
2007-10-07 17:16:07 +05:30
|
|
|
SYSLOG ((LOG_WARN, "cannot lock %s", grp_file));
|
2007-10-07 17:14:59 +05:30
|
|
|
closelog ();
|
|
|
|
exit (E_CANT_LOCK);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
#ifdef SHADOWGRP
|
2007-10-07 17:14:59 +05:30
|
|
|
if (is_shadow && !sgr_lock ()) {
|
|
|
|
fprintf (stderr, _("%s: cannot lock file %s\n"),
|
|
|
|
Prog, sgr_file);
|
2007-10-07 17:14:02 +05:30
|
|
|
if (optind == argc)
|
2007-10-07 17:16:07 +05:30
|
|
|
SYSLOG ((LOG_WARN, "cannot lock %s", sgr_file));
|
2007-10-07 17:14:59 +05:30
|
|
|
closelog ();
|
|
|
|
exit (E_CANT_LOCK);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2007-10-07 17:14:59 +05:30
|
|
|
* Open the files. Use O_RDONLY if we are in read_only mode,
|
2007-10-07 17:14:02 +05:30
|
|
|
* O_RDWR otherwise.
|
|
|
|
*/
|
2007-10-07 17:14:59 +05:30
|
|
|
if (!gr_open (read_only ? O_RDONLY : O_RDWR)) {
|
|
|
|
fprintf (stderr, _("%s: cannot open file %s\n"), Prog,
|
|
|
|
grp_file);
|
2007-10-07 17:14:02 +05:30
|
|
|
if (optind == argc)
|
2007-10-07 17:14:59 +05:30
|
|
|
SYSLOG ((LOG_WARN, "cannot open %s", grp_file));
|
|
|
|
closelog ();
|
|
|
|
exit (E_CANT_OPEN);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
#ifdef SHADOWGRP
|
2007-10-07 17:14:59 +05:30
|
|
|
if (is_shadow && !sgr_open (read_only ? O_RDONLY : O_RDWR)) {
|
|
|
|
fprintf (stderr, _("%s: cannot open file %s\n"), Prog,
|
|
|
|
sgr_file);
|
2007-10-07 17:14:02 +05:30
|
|
|
if (optind == argc)
|
2007-10-07 17:14:59 +05:30
|
|
|
SYSLOG ((LOG_WARN, "cannot open %s", sgr_file));
|
|
|
|
closelog ();
|
|
|
|
exit (E_CANT_OPEN);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-10-07 17:14:51 +05:30
|
|
|
if (sort_mode) {
|
2007-10-07 17:14:59 +05:30
|
|
|
gr_sort ();
|
2007-10-07 17:14:51 +05:30
|
|
|
#ifdef SHADOWGRP
|
|
|
|
if (is_shadow)
|
2007-10-07 17:14:59 +05:30
|
|
|
sgr_sort ();
|
2007-10-07 17:14:51 +05:30
|
|
|
#endif
|
|
|
|
goto write_and_bye;
|
|
|
|
}
|
|
|
|
|
2007-10-07 17:14:02 +05:30
|
|
|
/*
|
|
|
|
* Loop through the entire group file.
|
|
|
|
*/
|
2007-10-07 17:14:59 +05:30
|
|
|
for (gre = __gr_get_head (); gre; gre = gre->next) {
|
2007-10-07 17:14:02 +05:30
|
|
|
/*
|
|
|
|
* Skip all NIS entries.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (gre->line[0] == '+' || gre->line[0] == '-')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/*
|
2007-10-07 17:14:59 +05:30
|
|
|
* Start with the entries that are completely corrupt. They
|
|
|
|
* have no (struct group) entry because they couldn't be
|
|
|
|
* parsed properly.
|
2007-10-07 17:14:02 +05:30
|
|
|
*/
|
2007-10-07 17:14:14 +05:30
|
|
|
if (!gre->eptr) {
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
2007-10-07 17:14:59 +05:30
|
|
|
* Tell the user this entire line is bogus and ask
|
|
|
|
* them to delete it.
|
2007-10-07 17:14:02 +05:30
|
|
|
*/
|
2007-10-07 17:14:59 +05:30
|
|
|
printf (_("invalid group file entry\n"));
|
2007-10-07 17:17:33 +05:30
|
|
|
printf (_("delete line '%s'? "), gre->line);
|
2007-10-07 17:14:02 +05:30
|
|
|
errors++;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* prompt the user to delete the entry or not
|
|
|
|
*/
|
2007-10-07 17:14:59 +05:30
|
|
|
if (!yes_or_no ())
|
2007-10-07 17:14:02 +05:30
|
|
|
continue;
|
|
|
|
|
|
|
|
/*
|
2007-10-07 17:14:59 +05:30
|
|
|
* All group file deletions wind up here. This code
|
|
|
|
* removes the current entry from the linked list.
|
|
|
|
* When done, it skips back to the top of the loop
|
|
|
|
* to try out the next list element.
|
2007-10-07 17:14:02 +05:30
|
|
|
*/
|
2007-10-07 17:14:59 +05:30
|
|
|
delete_gr:
|
|
|
|
SYSLOG ((LOG_INFO, "delete group line `%s'",
|
|
|
|
gre->line));
|
2007-10-07 17:17:33 +05:30
|
|
|
changed++;
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
__gr_del_entry (gre);
|
2007-10-07 17:14:02 +05:30
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Group structure is good, start using it.
|
|
|
|
*/
|
2007-10-07 17:14:14 +05:30
|
|
|
grp = gre->eptr;
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* Make sure this entry has a unique name.
|
|
|
|
*/
|
2007-10-07 17:14:59 +05:30
|
|
|
for (tgre = __gr_get_head (); tgre; tgre = tgre->next) {
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-10-07 17:14:14 +05:30
|
|
|
const struct group *ent = tgre->eptr;
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* Don't check this entry
|
|
|
|
*/
|
|
|
|
if (tgre == gre)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Don't check invalid entries.
|
|
|
|
*/
|
|
|
|
if (!ent)
|
|
|
|
continue;
|
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
if (strcmp (grp->gr_name, ent->gr_name) != 0)
|
2007-10-07 17:14:02 +05:30
|
|
|
continue;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Tell the user this entry is a duplicate of
|
|
|
|
* another and ask them to delete it.
|
|
|
|
*/
|
2007-10-07 17:16:52 +05:30
|
|
|
printf (_("duplicate group entry\n"));
|
2007-10-07 17:17:33 +05:30
|
|
|
printf (_("delete line '%s'? "), gre->line);
|
2007-10-07 17:14:02 +05:30
|
|
|
errors++;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* prompt the user to delete the entry or not
|
|
|
|
*/
|
2007-10-07 17:14:59 +05:30
|
|
|
if (yes_or_no ())
|
2007-10-07 17:14:02 +05:30
|
|
|
goto delete_gr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check for invalid group names. --marekm
|
|
|
|
*/
|
2007-10-07 17:14:59 +05:30
|
|
|
if (!check_group_name (grp->gr_name)) {
|
2007-10-07 17:14:02 +05:30
|
|
|
errors++;
|
2007-10-07 17:17:33 +05:30
|
|
|
printf (_("invalid group name '%s'\n"), grp->gr_name);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Workaround for a NYS libc 5.3.12 bug on RedHat 4.2 -
|
2007-10-07 17:14:59 +05:30
|
|
|
* groups with no members are returned as groups with one
|
|
|
|
* member "", causing grpck to fail. --marekm
|
2007-10-07 17:14:02 +05:30
|
|
|
*/
|
2007-10-07 17:14:59 +05:30
|
|
|
if (grp->gr_mem[0] && !grp->gr_mem[1]
|
|
|
|
&& *(grp->gr_mem[0]) == '\0')
|
2007-10-07 17:14:02 +05:30
|
|
|
grp->gr_mem[0] = (char *) 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make sure each member exists
|
|
|
|
*/
|
|
|
|
for (i = 0; grp->gr_mem[i]; i++) {
|
2007-10-07 17:14:59 +05:30
|
|
|
if (getpwnam (grp->gr_mem[i]))
|
2007-10-07 17:14:02 +05:30
|
|
|
continue;
|
|
|
|
/*
|
2007-10-07 17:14:59 +05:30
|
|
|
* Can't find this user. Remove them
|
2007-10-07 17:14:02 +05:30
|
|
|
* from the list.
|
|
|
|
*/
|
|
|
|
errors++;
|
2007-10-07 17:14:59 +05:30
|
|
|
printf (_("group %s: no user %s\n"),
|
2007-10-07 17:14:02 +05:30
|
|
|
grp->gr_name, grp->gr_mem[i]);
|
2007-10-07 17:17:33 +05:30
|
|
|
printf (_("delete member '%s'? "), grp->gr_mem[i]);
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
if (!yes_or_no ())
|
2007-10-07 17:14:02 +05:30
|
|
|
continue;
|
|
|
|
|
2007-10-07 17:17:33 +05:30
|
|
|
SYSLOG ((LOG_INFO, "delete member '%s' group '%s'",
|
2007-10-07 17:14:59 +05:30
|
|
|
grp->gr_mem[i], grp->gr_name));
|
2007-10-07 17:17:33 +05:30
|
|
|
changed++;
|
2007-10-07 17:14:59 +05:30
|
|
|
delete_member (grp->gr_mem, grp->gr_mem[i]);
|
2007-10-07 17:14:02 +05:30
|
|
|
gre->changed = 1;
|
2007-10-07 17:14:59 +05:30
|
|
|
__gr_set_changed ();
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2007-10-07 17:17:33 +05:30
|
|
|
|
|
|
|
#ifdef SHADOWGRP
|
|
|
|
/*
|
|
|
|
* Make sure this entry exists in the /etc/gshadow file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (is_shadow) {
|
|
|
|
sgr = (struct sgrp *) sgr_locate (grp->gr_name);
|
|
|
|
if (sgr == NULL) {
|
|
|
|
printf (_
|
|
|
|
("no matching group file entry in %s\n"),
|
|
|
|
sgr_file);
|
|
|
|
printf (_("add group '%s' in %s ?"),
|
|
|
|
grp->gr_name, sgr_file);
|
|
|
|
errors++;
|
|
|
|
if (yes_or_no ()) {
|
|
|
|
struct sgrp sg;
|
|
|
|
struct group gr;
|
|
|
|
static char *empty = NULL;
|
|
|
|
|
|
|
|
sg.sg_name = grp->gr_name;
|
|
|
|
sg.sg_passwd = grp->gr_passwd;
|
|
|
|
sg.sg_adm = ∅
|
|
|
|
sg.sg_mem = grp->gr_mem;
|
|
|
|
SYSLOG ((LOG_INFO,
|
|
|
|
"add group `%s' to `%s'",
|
|
|
|
grp->gr_name, sgr_file));
|
|
|
|
changed++;
|
|
|
|
|
|
|
|
if (!sgr_update (&sg)) {
|
|
|
|
fprintf (stderr,
|
|
|
|
_
|
|
|
|
("%s: can't update shadow entry for %s\n"),
|
|
|
|
Prog, sg.sg_name);
|
|
|
|
exit (E_CANT_UPDATE);
|
|
|
|
}
|
|
|
|
/* remove password from /etc/group */
|
|
|
|
gr = *grp;
|
|
|
|
gr.gr_passwd = SHADOW_PASSWD_STRING; /* XXX warning: const */
|
|
|
|
if (!gr_update (&gr)) {
|
|
|
|
fprintf (stderr,
|
|
|
|
_
|
|
|
|
("%s: can't update entry for group %s\n"),
|
|
|
|
Prog, gr.gr_name);
|
|
|
|
exit (E_CANT_UPDATE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/**
|
|
|
|
* Verify that the all members defined in /etc/group are also
|
|
|
|
* present in /etc/gshadow.
|
|
|
|
*/
|
|
|
|
char **pgrp_mem, **psgr_mem;
|
|
|
|
|
|
|
|
for (pgrp_mem = grp->gr_mem; *pgrp_mem;
|
|
|
|
pgrp_mem++) {
|
|
|
|
for (psgr_mem = sgr->sg_mem; *psgr_mem;
|
|
|
|
psgr_mem++) {
|
|
|
|
if (strcmp
|
|
|
|
(*pgrp_mem, *psgr_mem) == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (*psgr_mem == NULL) {
|
|
|
|
printf
|
|
|
|
("'%s' is a member of the '%s' group in %s but not in %s\n",
|
|
|
|
*pgrp_mem, sgr->sg_name,
|
|
|
|
grp_file, sgr_file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef SHADOWGRP
|
|
|
|
if (!is_shadow)
|
|
|
|
goto shadow_done;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Loop through the entire shadow group file.
|
|
|
|
*/
|
2007-10-07 17:14:59 +05:30
|
|
|
for (sge = __sgr_get_head (); sge; sge = sge->next) {
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
2007-10-07 17:14:59 +05:30
|
|
|
* Start with the entries that are completely corrupt. They
|
|
|
|
* have no (struct sgrp) entry because they couldn't be
|
|
|
|
* parsed properly.
|
2007-10-07 17:14:02 +05:30
|
|
|
*/
|
2007-10-07 17:14:14 +05:30
|
|
|
if (!sge->eptr) {
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
2007-10-07 17:14:59 +05:30
|
|
|
* Tell the user this entire line is bogus and ask
|
|
|
|
* them to delete it.
|
2007-10-07 17:14:02 +05:30
|
|
|
*/
|
2007-10-07 17:14:59 +05:30
|
|
|
printf (_("invalid shadow group file entry\n"));
|
2007-10-07 17:17:33 +05:30
|
|
|
printf (_("delete line '%s'? "), sge->line);
|
2007-10-07 17:14:02 +05:30
|
|
|
errors++;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* prompt the user to delete the entry or not
|
|
|
|
*/
|
2007-10-07 17:14:59 +05:30
|
|
|
if (!yes_or_no ())
|
2007-10-07 17:14:02 +05:30
|
|
|
continue;
|
|
|
|
|
|
|
|
/*
|
2007-10-07 17:14:59 +05:30
|
|
|
* All shadow group file deletions wind up here.
|
2007-10-07 17:14:02 +05:30
|
|
|
* This code removes the current entry from the
|
2007-10-07 17:14:59 +05:30
|
|
|
* linked list. When done, it skips back to the top
|
|
|
|
* of the loop to try out the next list element.
|
2007-10-07 17:14:02 +05:30
|
|
|
*/
|
2007-10-07 17:14:59 +05:30
|
|
|
delete_sg:
|
|
|
|
SYSLOG ((LOG_INFO, "delete shadow line `%s'",
|
|
|
|
sge->line));
|
2007-10-07 17:17:33 +05:30
|
|
|
changed++;
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
__sgr_del_entry (sge);
|
2007-10-07 17:14:02 +05:30
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Shadow group structure is good, start using it.
|
|
|
|
*/
|
2007-10-07 17:14:14 +05:30
|
|
|
sgr = sge->eptr;
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* Make sure this entry has a unique name.
|
|
|
|
*/
|
2007-10-07 17:14:59 +05:30
|
|
|
for (tsge = __sgr_get_head (); tsge; tsge = tsge->next) {
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-10-07 17:14:14 +05:30
|
|
|
const struct sgrp *ent = tsge->eptr;
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* Don't check this entry
|
|
|
|
*/
|
|
|
|
if (tsge == sge)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Don't check invalid entries.
|
|
|
|
*/
|
|
|
|
if (!ent)
|
|
|
|
continue;
|
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
if (strcmp (sgr->sg_name, ent->sg_name) != 0)
|
2007-10-07 17:14:02 +05:30
|
|
|
continue;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Tell the user this entry is a duplicate of
|
|
|
|
* another and ask them to delete it.
|
|
|
|
*/
|
2007-10-07 17:16:52 +05:30
|
|
|
printf (_("duplicate shadow group entry\n"));
|
2007-10-07 17:17:33 +05:30
|
|
|
printf (_("delete line '%s'? "), sge->line);
|
2007-10-07 17:14:02 +05:30
|
|
|
errors++;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* prompt the user to delete the entry or not
|
|
|
|
*/
|
2007-10-07 17:14:59 +05:30
|
|
|
if (yes_or_no ())
|
2007-10-07 17:14:02 +05:30
|
|
|
goto delete_sg;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make sure this entry exists in the /etc/group file.
|
|
|
|
*/
|
2007-10-07 17:17:33 +05:30
|
|
|
grp = (struct group *) gr_locate (sgr->sg_name);
|
|
|
|
if (grp == NULL) {
|
|
|
|
printf (_("no matching group file entry in %s\n"),
|
|
|
|
grp_file);
|
|
|
|
printf (_("delete line '%s'? "), sge->line);
|
2007-10-07 17:14:02 +05:30
|
|
|
errors++;
|
2007-10-07 17:14:59 +05:30
|
|
|
if (yes_or_no ())
|
2007-10-07 17:14:02 +05:30
|
|
|
goto delete_sg;
|
2007-10-07 17:17:33 +05:30
|
|
|
} else {
|
|
|
|
/**
|
|
|
|
* Verify that the all members defined in /etc/gshadow are also
|
|
|
|
* present in /etc/group.
|
|
|
|
*/
|
|
|
|
char **pgrp_mem, **psgr_mem;
|
|
|
|
|
|
|
|
for (psgr_mem = sgr->sg_mem; *psgr_mem; psgr_mem++) {
|
|
|
|
for (pgrp_mem = grp->gr_mem; *pgrp_mem;
|
|
|
|
pgrp_mem++) {
|
|
|
|
if (strcmp (*pgrp_mem, *psgr_mem) == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (*pgrp_mem == NULL) {
|
|
|
|
printf
|
|
|
|
("'%s' is a member of the '%s' group in %s but not in %s\n",
|
|
|
|
*psgr_mem, sgr->sg_name, sgr_file,
|
|
|
|
grp_file);
|
|
|
|
}
|
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make sure each administrator exists
|
|
|
|
*/
|
|
|
|
for (i = 0; sgr->sg_adm[i]; i++) {
|
2007-10-07 17:14:59 +05:30
|
|
|
if (getpwnam (sgr->sg_adm[i]))
|
2007-10-07 17:14:02 +05:30
|
|
|
continue;
|
|
|
|
/*
|
2007-10-07 17:14:59 +05:30
|
|
|
* Can't find this user. Remove them
|
2007-10-07 17:14:02 +05:30
|
|
|
* from the list.
|
|
|
|
*/
|
|
|
|
errors++;
|
2007-10-07 17:14:59 +05:30
|
|
|
printf (_
|
|
|
|
("shadow group %s: no administrative user %s\n"),
|
2007-10-07 17:14:02 +05:30
|
|
|
sgr->sg_name, sgr->sg_adm[i]);
|
2007-10-07 17:17:33 +05:30
|
|
|
printf (_("delete administrative member '%s'? "),
|
2007-10-07 17:14:59 +05:30
|
|
|
sgr->sg_adm[i]);
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
if (!yes_or_no ())
|
2007-10-07 17:14:02 +05:30
|
|
|
continue;
|
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
SYSLOG ((LOG_INFO,
|
|
|
|
"delete admin `%s' from shadow group `%s'",
|
|
|
|
sgr->sg_adm[i], sgr->sg_name));
|
2007-10-07 17:17:33 +05:30
|
|
|
changed++;
|
2007-10-07 17:14:59 +05:30
|
|
|
delete_member (sgr->sg_adm, sgr->sg_adm[i]);
|
2007-10-07 17:14:02 +05:30
|
|
|
sge->changed = 1;
|
2007-10-07 17:14:59 +05:30
|
|
|
__sgr_set_changed ();
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make sure each member exists
|
|
|
|
*/
|
|
|
|
for (i = 0; sgr->sg_mem[i]; i++) {
|
2007-10-07 17:14:59 +05:30
|
|
|
if (getpwnam (sgr->sg_mem[i]))
|
2007-10-07 17:14:02 +05:30
|
|
|
continue;
|
|
|
|
|
|
|
|
/*
|
2007-10-07 17:14:59 +05:30
|
|
|
* Can't find this user. Remove them from the list.
|
2007-10-07 17:14:02 +05:30
|
|
|
*/
|
|
|
|
errors++;
|
2007-10-07 17:14:59 +05:30
|
|
|
printf (_("shadow group %s: no user %s\n"),
|
2007-10-07 17:14:02 +05:30
|
|
|
sgr->sg_name, sgr->sg_mem[i]);
|
2007-10-07 17:17:33 +05:30
|
|
|
printf (_("delete member '%s'? "), sgr->sg_mem[i]);
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
if (!yes_or_no ())
|
2007-10-07 17:14:02 +05:30
|
|
|
continue;
|
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
SYSLOG ((LOG_INFO,
|
|
|
|
"delete member `%s' from shadow group `%s'",
|
|
|
|
sgr->sg_mem[i], sgr->sg_name));
|
2007-10-07 17:17:33 +05:30
|
|
|
changed++;
|
2007-10-07 17:14:59 +05:30
|
|
|
delete_member (sgr->sg_mem, sgr->sg_mem[i]);
|
2007-10-07 17:14:02 +05:30
|
|
|
sge->changed = 1;
|
2007-10-07 17:14:59 +05:30
|
|
|
__sgr_set_changed ();
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
shadow_done:
|
|
|
|
#endif /* SHADOWGRP */
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
2007-10-07 17:17:33 +05:30
|
|
|
* All done. If there were no change we can just abandon any
|
2007-10-07 17:14:02 +05:30
|
|
|
* changes to the files.
|
|
|
|
*/
|
2007-10-07 17:17:33 +05:30
|
|
|
if (changed) {
|
2007-10-07 17:14:59 +05:30
|
|
|
write_and_bye:
|
|
|
|
if (!gr_close ()) {
|
|
|
|
fprintf (stderr, _("%s: cannot update file %s\n"),
|
|
|
|
Prog, grp_file);
|
|
|
|
exit (E_CANT_UPDATE);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
#ifdef SHADOWGRP
|
2007-10-07 17:14:59 +05:30
|
|
|
if (is_shadow && !sgr_close ()) {
|
|
|
|
fprintf (stderr, _("%s: cannot update file %s\n"),
|
|
|
|
Prog, sgr_file);
|
|
|
|
exit (E_CANT_UPDATE);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Don't be anti-social - unlock the files when you're done.
|
|
|
|
*/
|
|
|
|
#ifdef SHADOWGRP
|
|
|
|
if (is_shadow)
|
2007-10-07 17:14:59 +05:30
|
|
|
sgr_unlock ();
|
2007-10-07 17:14:02 +05:30
|
|
|
#endif
|
2007-10-07 17:14:59 +05:30
|
|
|
(void) gr_unlock ();
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-10-07 17:16:52 +05:30
|
|
|
nscd_flush_cache ("group");
|
|
|
|
|
2007-10-07 17:14:02 +05:30
|
|
|
/*
|
|
|
|
* Tell the user what we did and exit.
|
|
|
|
*/
|
|
|
|
if (errors)
|
2007-10-07 17:17:33 +05:30
|
|
|
printf (changed ?
|
2007-10-07 17:14:02 +05:30
|
|
|
_("%s: the files have been updated\n") :
|
|
|
|
_("%s: no changes\n"), Prog);
|
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
exit (errors ? E_BAD_ENTRY : E_OKAY);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|