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;
|
2008-01-01 18:43:47 +05:30
|
|
|
static int use_system_grp_file = 1;
|
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;
|
2008-01-01 18:43:47 +05:30
|
|
|
static int use_system_sgr_file = 1;
|
|
|
|
static int is_shadow = 0;
|
2007-10-07 17:14:02 +05:30
|
|
|
#endif
|
2008-01-01 18:43:47 +05:30
|
|
|
/* Options */
|
2007-10-07 17:14:02 +05:30
|
|
|
static int read_only = 0;
|
2008-01-01 18:43:47 +05:30
|
|
|
static int sort_mode = 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 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
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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];
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2008-01-01 18:43:47 +05:30
|
|
|
* process_flags -
|
2007-10-07 17:14:02 +05:30
|
|
|
*/
|
2008-01-01 18:43:47 +05:30
|
|
|
static void process_flags (int argc, char **argv)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:14:59 +05:30
|
|
|
int arg;
|
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
|
2008-01-01 18:43:47 +05:30
|
|
|
if ((argc < optind) || (argc > (optind + 2)))
|
2007-10-07 17:14:02 +05:30
|
|
|
#else
|
2008-01-01 18:43:47 +05:30
|
|
|
if ((argc < optind) || (argc > (optind + 1)))
|
2007-10-07 17:14:02 +05:30
|
|
|
#endif
|
2008-01-01 18:43:47 +05:30
|
|
|
{
|
2007-10-07 17:14:59 +05:30
|
|
|
usage ();
|
2008-01-01 18:43:47 +05:30
|
|
|
}
|
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);
|
2008-01-01 18:43:47 +05:30
|
|
|
use_system_grp_file = 0;
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
#ifdef SHADOWGRP
|
2008-01-01 18:43:47 +05:30
|
|
|
if ((optind + 2) == argc) {
|
2007-10-07 17:14:02 +05:30
|
|
|
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;
|
2008-01-01 18:43:47 +05:30
|
|
|
use_system_sgr_file = 0;
|
|
|
|
} else if (optind == argc) {
|
2007-10-07 17:14:59 +05:30
|
|
|
is_shadow = sgr_file_present ();
|
2008-01-01 18:43:47 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
#endif
|
2008-01-01 18:43:47 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2008-01-01 18:43:47 +05:30
|
|
|
static void open_files ()
|
|
|
|
{
|
2007-10-07 17:14:02 +05:30
|
|
|
/*
|
|
|
|
* Lock the files if we aren't in "read-only" mode
|
|
|
|
*/
|
|
|
|
if (!read_only) {
|
2008-01-01 18:43:47 +05:30
|
|
|
if (gr_lock () == 0) {
|
2007-10-07 17:14:59 +05:30
|
|
|
fprintf (stderr, _("%s: cannot lock file %s\n"),
|
2008-01-01 18:43:47 +05:30
|
|
|
Prog, grp_file);
|
|
|
|
if (use_system_grp_file) {
|
2007-10-07 17:16:07 +05:30
|
|
|
SYSLOG ((LOG_WARN, "cannot lock %s", grp_file));
|
2008-01-01 18:43:47 +05:30
|
|
|
}
|
2007-10-07 17:14:59 +05:30
|
|
|
closelog ();
|
|
|
|
exit (E_CANT_LOCK);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
#ifdef SHADOWGRP
|
2008-01-01 18:43:47 +05:30
|
|
|
if (is_shadow && (sgr_lock () == 0)) {
|
2007-10-07 17:14:59 +05:30
|
|
|
fprintf (stderr, _("%s: cannot lock file %s\n"),
|
2008-01-01 18:43:47 +05:30
|
|
|
Prog, sgr_file);
|
|
|
|
if (use_system_sgr_file) {
|
2007-10-07 17:16:07 +05:30
|
|
|
SYSLOG ((LOG_WARN, "cannot lock %s", sgr_file));
|
2008-01-01 18:43:47 +05:30
|
|
|
}
|
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.
|
|
|
|
*/
|
2008-01-01 18:43:47 +05:30
|
|
|
if (gr_open (read_only ? O_RDONLY : O_RDWR) == 0) {
|
2007-10-07 17:14:59 +05:30
|
|
|
fprintf (stderr, _("%s: cannot open file %s\n"), Prog,
|
2008-01-01 18:43:47 +05:30
|
|
|
grp_file);
|
|
|
|
if (use_system_grp_file) {
|
2007-10-07 17:14:59 +05:30
|
|
|
SYSLOG ((LOG_WARN, "cannot open %s", grp_file));
|
2008-01-01 18:43:47 +05:30
|
|
|
}
|
2007-10-07 17:14:59 +05:30
|
|
|
closelog ();
|
|
|
|
exit (E_CANT_OPEN);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
#ifdef SHADOWGRP
|
2008-01-01 18:43:47 +05:30
|
|
|
if (is_shadow && (sgr_open (read_only ? O_RDONLY : O_RDWR) == 0)) {
|
2007-10-07 17:14:59 +05:30
|
|
|
fprintf (stderr, _("%s: cannot open file %s\n"), Prog,
|
2008-01-01 18:43:47 +05:30
|
|
|
sgr_file);
|
|
|
|
if (use_system_sgr_file) {
|
2007-10-07 17:14:59 +05:30
|
|
|
SYSLOG ((LOG_WARN, "cannot open %s", sgr_file));
|
2008-01-01 18:43:47 +05:30
|
|
|
}
|
2007-10-07 17:14:59 +05:30
|
|
|
closelog ();
|
|
|
|
exit (E_CANT_OPEN);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
#endif
|
2008-01-01 18:43:47 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static void close_files (int changed)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* All done. If there were no change we can just abandon any
|
|
|
|
* changes to the files.
|
|
|
|
*/
|
|
|
|
if (changed) {
|
|
|
|
if (gr_close () == 0) {
|
|
|
|
fprintf (stderr, _("%s: cannot update file %s\n"),
|
|
|
|
Prog, grp_file);
|
|
|
|
exit (E_CANT_UPDATE);
|
|
|
|
}
|
|
|
|
#ifdef SHADOWGRP
|
|
|
|
if (is_shadow && (sgr_close () == 0)) {
|
|
|
|
fprintf (stderr, _("%s: cannot update file %s\n"),
|
|
|
|
Prog, sgr_file);
|
|
|
|
exit (E_CANT_UPDATE);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Don't be anti-social - unlock the files when you're done.
|
|
|
|
*/
|
|
|
|
#ifdef SHADOWGRP
|
|
|
|
if (is_shadow) {
|
|
|
|
sgr_unlock ();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
(void) gr_unlock ();
|
|
|
|
}
|
|
|
|
|
2008-01-01 19:20:06 +05:30
|
|
|
static int check_members (const char *groupname,
|
|
|
|
char **members,
|
|
|
|
const char *fmt_info,
|
|
|
|
const char *fmt_prompt,
|
|
|
|
const char *fmt_syslog,
|
|
|
|
int *errors)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int members_changed = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make sure each member exists
|
|
|
|
*/
|
|
|
|
for (i = 0; members[i]; i++) {
|
|
|
|
/* local, no need for xgetpwnam */
|
|
|
|
if (getpwnam (members[i]))
|
|
|
|
continue;
|
|
|
|
/*
|
|
|
|
* Can't find this user. Remove them
|
|
|
|
* from the list.
|
|
|
|
*/
|
|
|
|
*errors += 1;
|
|
|
|
printf (fmt_info, groupname, members[i]);
|
|
|
|
printf (fmt_prompt, members[i]);
|
|
|
|
|
|
|
|
if (!yes_or_no (read_only))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
SYSLOG ((LOG_INFO, fmt_syslog, members[i], groupname));
|
|
|
|
members_changed = 1;
|
|
|
|
delete_member (members, members[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return members_changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void compare_members_lists (const char *groupname,
|
|
|
|
char **members,
|
|
|
|
char **other_members,
|
|
|
|
const char *file,
|
|
|
|
const char *other_file)
|
|
|
|
{
|
|
|
|
char **pmem, **other_pmem;
|
|
|
|
|
|
|
|
for (pmem = members; *pmem; pmem++) {
|
|
|
|
for (other_pmem = other_members; *other_pmem; other_pmem++) {
|
|
|
|
if (strcmp (*pmem, *other_pmem) == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (*other_pmem == NULL) {
|
|
|
|
printf
|
|
|
|
("'%s' is a member of the '%s' group in %s but not in %s\n",
|
|
|
|
*pmem, groupname, file, other_file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-01 19:18:49 +05:30
|
|
|
static void check_grp_file (int *errors, int *changed)
|
2008-01-01 18:43:47 +05:30
|
|
|
{
|
|
|
|
struct commonio_entry *gre, *tgre;
|
|
|
|
struct group *grp;
|
2008-01-01 19:18:49 +05:30
|
|
|
#ifdef SHADOWGRP
|
2008-01-01 18:43:47 +05:30
|
|
|
struct sgrp *sgr;
|
|
|
|
#endif
|
|
|
|
|
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);
|
2008-01-01 19:18:49 +05:30
|
|
|
*errors += 1;
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* prompt the user to delete the entry or not
|
|
|
|
*/
|
2007-12-26 22:20:38 +05:30
|
|
|
if (!yes_or_no (read_only))
|
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));
|
2008-01-01 19:18:49 +05:30
|
|
|
*changed = 1;
|
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);
|
2008-01-01 19:18:49 +05:30
|
|
|
*errors += 1;
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* prompt the user to delete the entry or not
|
|
|
|
*/
|
2007-12-26 22:20:38 +05:30
|
|
|
if (yes_or_no (read_only))
|
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)) {
|
2008-01-01 19:18:49 +05:30
|
|
|
*errors += 1;
|
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;
|
|
|
|
|
2008-01-01 19:20:06 +05:30
|
|
|
if (check_members (grp->gr_name, grp->gr_mem,
|
|
|
|
_("group %s: no user %s\n"),
|
|
|
|
_("delete member '%s'? "),
|
|
|
|
"delete member `%s' from group `%s'",
|
|
|
|
errors) == 1) {
|
2008-01-01 19:18:49 +05:30
|
|
|
*changed = 1;
|
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);
|
2008-01-01 19:18:49 +05:30
|
|
|
*errors += 1;
|
2007-12-26 22:20:38 +05:30
|
|
|
if (yes_or_no (read_only)) {
|
2007-10-07 17:17:33 +05:30
|
|
|
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));
|
2008-01-01 19:18:49 +05:30
|
|
|
*changed = 1;
|
2007-10-07 17:17:33 +05:30
|
|
|
|
|
|
|
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 {
|
|
|
|
/**
|
2008-01-01 19:20:06 +05:30
|
|
|
* Verify that all the members defined in /etc/group are also
|
2007-10-07 17:17:33 +05:30
|
|
|
* present in /etc/gshadow.
|
|
|
|
*/
|
2008-01-01 19:20:06 +05:30
|
|
|
compare_members_lists (grp->gr_name,
|
|
|
|
grp->gr_mem, sgr->sg_mem,
|
|
|
|
grp_file, sgr_file);
|
2007-10-07 17:17:33 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2008-01-01 19:18:49 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2008-01-01 19:18:49 +05:30
|
|
|
#ifdef SHADOWGRP
|
|
|
|
static void check_sgr_file (int *errors, int *changed)
|
|
|
|
{
|
|
|
|
struct group *grp;
|
|
|
|
struct commonio_entry *sge, *tsge;
|
|
|
|
struct sgrp *sgr;
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* 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);
|
2008-01-01 19:18:49 +05:30
|
|
|
*errors += 1;
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* prompt the user to delete the entry or not
|
|
|
|
*/
|
2007-12-26 22:20:38 +05:30
|
|
|
if (!yes_or_no (read_only))
|
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));
|
2008-01-01 19:18:49 +05:30
|
|
|
*changed = 1;
|
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);
|
2008-01-01 19:18:49 +05:30
|
|
|
*errors += 1;
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* prompt the user to delete the entry or not
|
|
|
|
*/
|
2007-12-26 22:20:38 +05:30
|
|
|
if (yes_or_no (read_only))
|
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);
|
2008-01-01 19:18:49 +05:30
|
|
|
*errors += 1;
|
2007-12-26 22:20:38 +05:30
|
|
|
if (yes_or_no (read_only))
|
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.
|
|
|
|
*/
|
2008-01-01 19:20:06 +05:30
|
|
|
compare_members_lists (sgr->sg_name,
|
|
|
|
sgr->sg_mem, grp->gr_mem,
|
|
|
|
sgr_file, grp_file);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make sure each administrator exists
|
|
|
|
*/
|
2008-01-01 19:20:06 +05:30
|
|
|
if (check_members (sgr->sg_name, sgr->sg_adm,
|
|
|
|
_("shadow group %s: no administrative user %s\n"),
|
|
|
|
_("delete administrative member '%s'? "),
|
|
|
|
"delete admin `%s' from shadow group `%s'",
|
|
|
|
errors) == 1) {
|
2008-01-01 19:18:49 +05:30
|
|
|
*changed = 1;
|
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
|
|
|
|
*/
|
2008-01-01 19:20:06 +05:30
|
|
|
if (check_members (sgr->sg_name, sgr->sg_mem,
|
|
|
|
_("shadow group %s: no user %s\n"),
|
|
|
|
_("delete member '%s'? "),
|
|
|
|
"delete member `%s' from shadow group `%s'",
|
|
|
|
errors) == 1) {
|
2008-01-01 19:18:49 +05:30
|
|
|
*changed = 1;
|
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
|
|
|
}
|
|
|
|
}
|
2008-01-01 19:18:49 +05:30
|
|
|
}
|
2007-10-07 17:14:59 +05:30
|
|
|
#endif /* SHADOWGRP */
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2008-01-01 19:18:49 +05:30
|
|
|
/*
|
|
|
|
* grpck - verify group file integrity
|
|
|
|
*/
|
|
|
|
int main (int argc, char **argv)
|
|
|
|
{
|
|
|
|
int errors = 0;
|
|
|
|
int changed = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get my name so that I can use it to report errors.
|
|
|
|
*/
|
|
|
|
Prog = Basename (argv[0]);
|
|
|
|
|
|
|
|
setlocale (LC_ALL, "");
|
|
|
|
bindtextdomain (PACKAGE, LOCALEDIR);
|
|
|
|
textdomain (PACKAGE);
|
|
|
|
|
|
|
|
OPENLOG ("grpck");
|
|
|
|
|
|
|
|
/* Parse the command line arguments */
|
|
|
|
process_flags (argc, argv);
|
|
|
|
|
|
|
|
open_files ();
|
|
|
|
|
|
|
|
if (sort_mode) {
|
|
|
|
gr_sort ();
|
|
|
|
#ifdef SHADOWGRP
|
|
|
|
if (is_shadow)
|
|
|
|
sgr_sort ();
|
|
|
|
changed = 1;
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
check_grp_file (&errors, &changed);
|
|
|
|
#ifdef SHADOWGRP
|
|
|
|
if (is_shadow) {
|
|
|
|
check_sgr_file (&errors, &changed);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2008-01-01 18:43:47 +05:30
|
|
|
/* Commit the change in the database if needed */
|
|
|
|
close_files (changed);
|
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
|
|
|
}
|