/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T
* All Rights Reserved
*/
/*
* University Copyright- Copyright (c) 1982, 1986, 1988
* The Regents of the University of California
* All Rights Reserved
*
* University Acknowledgment- Portions of this document are derived from
* software developed by the University of California, Berkeley, and its
* contributors.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* chgrp [-fhR] gid file ...
* chgrp -R [-f] [-H|-L|-P] gid file ...
* chgrp -s [-fhR] groupsid file ...
* chgrp -s -R [-f] [-H|-L|-P] groupsid file ...
*/
#include <stdio.h>
#include <ctype.h>
#include <grp.h>
#include <dirent.h>
#include <unistd.h>
#include <stdlib.h>
#include <locale.h>
#include <libcmdutils.h>
#include <errno.h>
#include <strings.h>
#include <aclutils.h>
static int hflag = 0,
fflag = 0,
rflag = 0,
Hflag = 0,
Lflag = 0,
Pflag = 0,
sflag = 0;
static void usage(void);
static int isnumber(char *);
static int Perror(char *);
#ifdef XPG4
/*
* Check to see if we are to follow symlinks specified on the command line.
* This assumes we've already checked to make sure neither -h or -P was
* specified, so we are just looking to see if -R -L, or -R -H was specified,
* or, since -R has the same behavior as -R -L, if -R was specified by itself.
* Therefore, all we really need to check for is if -R was specified.
*/
#else
/*
* Check to see if we are to follow symlinks specified on the command line.
* This assumes we've already checked to make sure neither -h or -P was
* specified, so we are just looking to see if -R -L, or -R -H was specified.
* Note: -R by itself will change the group of a directory referenced by a
* symlink however it will not follow the symlink to any other part of the
* file hierarchy.
*/
#endif
#ifdef XPG4
/*
* Follow symlinks when traversing directories. Since -R behaves the
* same as -R -L, we always want to follow symlinks to other parts
* of the file hierarchy unless -H was specified.
*/
#else
/*
* Follow symlinks when traversing directories. Only follow symlinks
* to other parts of the file hierarchy if -L was specified.
*/
#endif
}
}
/*
* We're ignoring errors here because preserving the SET[UG]ID bits is just
* a courtesy. This is only used on directories.
*/
extern int optind;
int
{
int c;
/* set the locale for only the messages system (all else is clean) */
#if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
#endif
(void) textdomain(TEXT_DOMAIN);
switch (c) {
case 'R':
rflag++;
break;
case 'h':
hflag++;
break;
case 'f':
fflag++;
break;
case 'H':
/*
* If more than one of -H, -L, and -P
* are specified, only the last option
* specified determines the behavior of
* chgrp. In addition, make [-H|-L]
* mutually exclusive of -h.
*/
Hflag++;
break;
case 'L':
Lflag++;
break;
case 'P':
Pflag++;
break;
case 's':
sflag++;
break;
default:
usage();
}
/*
* Check for sufficient arguments
* or a usage error.
*/
if ((argc < 2) ||
usage();
}
if (sflag) {
"chgrp: invalid group sid %s\n"), argv[0]);
exit(2);
}
} else {
errno = 0;
/* gid is an int */
if (errno != 0) {
"chgrp: group id is too large\n"));
exit(2);
} else {
"chgrp: invalid group id\n"));
exit(2);
}
}
} else {
argv[0]);
exit(2);
}
}
for (c = 1; c < argc; c++) {
continue;
}
/*
* Change the group id of the symbolic link
* specified on the command line.
* Don't follow the symbolic link to
* any other part of the file hierarchy.
*/
} else {
continue;
}
/*
* We know that we are to change the
* group of the file referenced by the
* symlink specified on the command line.
* Now check to see if we are to follow
* the symlink to any other part of the
* file hierarchy.
*/
if (FOLLOW_CL_LINKS) {
== S_IFDIR) {
/*
* We are following symlinks so
* traverse into the directory.
* Add this node to the search
* tree so we don't get into an
* endless loop.
*/
/*
* Try to restore the
* SET[UG]ID bits.
*/
argv[c],
~S_IFMT);
} else {
/*
* Error occurred.
* rc can't be 0
* as this is the first
* node to be added to
* the search tree.
*/
argv[c]);
}
} else {
/*
* Change the group id of the
* file referenced by the
* symbolic link.
*/
}
} else {
/*
* Change the group id of the file
* referenced by the symbolic link.
*/
== S_IFDIR) {
/* Reset the SET[UG]ID bits. */
SETUGID_PRESERVE(argv[c],
}
}
}
/*
* Add this node to the search tree so we don't
* get into a endless loop.
*/
/* Restore the SET[UG]ID bits. */
SETUGID_PRESERVE(argv[c],
} else {
/*
* An error occurred while trying
* to add the node to the tree.
* Continue on with next file
* specified. Note: rc shouldn't
* be 0 as this was the first node
* being added to the search tree.
*/
}
} else {
} else {
}
/* If a directory, reset the SET[UG]ID bits. */
SETUGID_PRESERVE(argv[c],
}
}
}
return (status);
}
/*
* chgrpr() - recursive chown()
*
* Recursively chowns the input directory then its contents. rflag must
* have been set if chgrpr() is called. The input directory should not
* be a sym link (this is handled in the calling routine). In
* addition, the calling routine should have already added the input
* directory to the search tree so we do not get into endless loops.
* Note: chgrpr() doesn't need a return value as errors are reported
* through the global "status" variable.
*/
static void
{
exit(255);
}
/*
* Attempt to chown the directory, however don't return if we
* can't as we still may be able to chown the contents of the
* directory. Note: the calling routine resets the SUID bits
* on this directory so we don't have to perform an extra 'stat'.
*/
return;
}
return;
}
continue; /* skip "." and ".." */
}
continue;
}
/*
* Change the group id of the symbolic link
* encountered while traversing the
* directory. Don't follow the symbolic
* link to any other part of the file
* hierarchy.
*/
} else {
continue;
}
/*
* We know that we are to change the
* group of the file referenced by the
* symlink encountered while traversing
* the directory. Now check to see if we
* are to follow the symlink to any other
* part of the file hierarchy.
*/
if (FOLLOW_D_LINKS) {
/*
* We are following symlinks so
* traverse into the directory.
* Add this node to the search
* tree so we don't get into an
* endless loop.
*/
int rc;
/*
* Restore SET[UG]ID
* bits.
*/
~S_IFMT);
} else if (rc == 0) {
/* already visited */
continue;
} else {
/*
* An error occurred
* while trying to add
* the node to the tree.
*/
continue;
}
} else {
/*
* Change the group id of the
* file referenced by the
* symbolic link.
*/
}
} else {
/*
* Change the group id of the file
* referenced by the symbolic link.
*/
/* Restore SET[UG]ID bits. */
}
}
}
/*
* Add this node to the search tree so we don't
* get into a endless loop.
*/
int rc;
/* Restore the SET[UG]ID bits. */
} else if (rc == 0) {
/* already visited */
continue;
} else {
/*
* An error occurred while trying
* to add the node to the search tree.
*/
continue;
}
} else {
}
}
savedir);
exit(255);
}
}
static int
isnumber(char *s)
{
int c;
while ((c = *s++) != '\0')
if (!isdigit(c))
return (0);
return (1);
}
static int
Perror(char *s)
{
if (!fflag) {
perror(s);
}
return (!fflag);
}
static void
usage(void)
{
"usage:\n"
"\tchgrp [-fhR] group file ...\n"
"\tchgrp -R [-f] [-H|-L|-P] group file ...\n"
"\tchgrp -s [-fhR] groupsid file ...\n"
"\tchgrp -s -R [-f] [-H|-L|-P] groupsid file ...\n"));
exit(2);
}