17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5bda8495cSas145665 * Common Development and Distribution License (the "License").
6bda8495cSas145665 * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /*
22*b249c65cSmarks * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate * Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T
287c478bd9Sstevel@tonic-gate * All Rights Reserved
297c478bd9Sstevel@tonic-gate */
307c478bd9Sstevel@tonic-gate
317c478bd9Sstevel@tonic-gate /*
327c478bd9Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988
337c478bd9Sstevel@tonic-gate * The Regents of the University of California
347c478bd9Sstevel@tonic-gate * All Rights Reserved
357c478bd9Sstevel@tonic-gate *
367c478bd9Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from
377c478bd9Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its
387c478bd9Sstevel@tonic-gate * contributors.
397c478bd9Sstevel@tonic-gate */
407c478bd9Sstevel@tonic-gate
417c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
427c478bd9Sstevel@tonic-gate
437c478bd9Sstevel@tonic-gate /*
447c478bd9Sstevel@tonic-gate * chgrp [-fhR] gid file ...
457c478bd9Sstevel@tonic-gate * chgrp -R [-f] [-H|-L|-P] gid file ...
46*b249c65cSmarks * chgrp -s [-fhR] groupsid file ...
47*b249c65cSmarks * chgrp -s -R [-f] [-H|-L|-P] groupsid file ...
487c478bd9Sstevel@tonic-gate */
497c478bd9Sstevel@tonic-gate
507c478bd9Sstevel@tonic-gate #include <stdio.h>
517c478bd9Sstevel@tonic-gate #include <ctype.h>
527c478bd9Sstevel@tonic-gate #include <sys/types.h>
537c478bd9Sstevel@tonic-gate #include <sys/stat.h>
547c478bd9Sstevel@tonic-gate #include <sys/avl.h>
557c478bd9Sstevel@tonic-gate #include <grp.h>
567c478bd9Sstevel@tonic-gate #include <dirent.h>
577c478bd9Sstevel@tonic-gate #include <unistd.h>
587c478bd9Sstevel@tonic-gate #include <stdlib.h>
597c478bd9Sstevel@tonic-gate #include <locale.h>
607c478bd9Sstevel@tonic-gate #include <libcmdutils.h>
617c478bd9Sstevel@tonic-gate #include <errno.h>
627c478bd9Sstevel@tonic-gate #include <strings.h>
63*b249c65cSmarks #include <aclutils.h>
647c478bd9Sstevel@tonic-gate
657c478bd9Sstevel@tonic-gate static struct group *gr;
667c478bd9Sstevel@tonic-gate static struct stat stbuf;
677c478bd9Sstevel@tonic-gate static struct stat stbuf2;
687c478bd9Sstevel@tonic-gate static gid_t gid;
697c478bd9Sstevel@tonic-gate static int hflag = 0,
707c478bd9Sstevel@tonic-gate fflag = 0,
717c478bd9Sstevel@tonic-gate rflag = 0,
727c478bd9Sstevel@tonic-gate Hflag = 0,
737c478bd9Sstevel@tonic-gate Lflag = 0,
74*b249c65cSmarks Pflag = 0,
75*b249c65cSmarks sflag = 0;
767c478bd9Sstevel@tonic-gate static int status = 0; /* total number of errors received */
777c478bd9Sstevel@tonic-gate
787c478bd9Sstevel@tonic-gate static avl_tree_t *tree; /* search tree to store inode data */
797c478bd9Sstevel@tonic-gate
807c478bd9Sstevel@tonic-gate static void usage(void);
817c478bd9Sstevel@tonic-gate static int isnumber(char *);
827c478bd9Sstevel@tonic-gate static int Perror(char *);
837c478bd9Sstevel@tonic-gate static void chgrpr(char *, gid_t);
847c478bd9Sstevel@tonic-gate
857c478bd9Sstevel@tonic-gate #ifdef XPG4
867c478bd9Sstevel@tonic-gate /*
877c478bd9Sstevel@tonic-gate * Check to see if we are to follow symlinks specified on the command line.
887c478bd9Sstevel@tonic-gate * This assumes we've already checked to make sure neither -h or -P was
897c478bd9Sstevel@tonic-gate * specified, so we are just looking to see if -R -L, or -R -H was specified,
907c478bd9Sstevel@tonic-gate * or, since -R has the same behavior as -R -L, if -R was specified by itself.
917c478bd9Sstevel@tonic-gate * Therefore, all we really need to check for is if -R was specified.
927c478bd9Sstevel@tonic-gate */
937c478bd9Sstevel@tonic-gate #define FOLLOW_CL_LINKS (rflag)
947c478bd9Sstevel@tonic-gate #else
957c478bd9Sstevel@tonic-gate /*
967c478bd9Sstevel@tonic-gate * Check to see if we are to follow symlinks specified on the command line.
977c478bd9Sstevel@tonic-gate * This assumes we've already checked to make sure neither -h or -P was
987c478bd9Sstevel@tonic-gate * specified, so we are just looking to see if -R -L, or -R -H was specified.
997c478bd9Sstevel@tonic-gate * Note: -R by itself will change the group of a directory referenced by a
1007c478bd9Sstevel@tonic-gate * symlink however it will not follow the symlink to any other part of the
1017c478bd9Sstevel@tonic-gate * file hierarchy.
1027c478bd9Sstevel@tonic-gate */
1037c478bd9Sstevel@tonic-gate #define FOLLOW_CL_LINKS (rflag && (Hflag || Lflag))
1047c478bd9Sstevel@tonic-gate #endif
1057c478bd9Sstevel@tonic-gate
1067c478bd9Sstevel@tonic-gate #ifdef XPG4
1077c478bd9Sstevel@tonic-gate /*
1087c478bd9Sstevel@tonic-gate * Follow symlinks when traversing directories. Since -R behaves the
1097c478bd9Sstevel@tonic-gate * same as -R -L, we always want to follow symlinks to other parts
1107c478bd9Sstevel@tonic-gate * of the file hierarchy unless -H was specified.
1117c478bd9Sstevel@tonic-gate */
1127c478bd9Sstevel@tonic-gate #define FOLLOW_D_LINKS (!Hflag)
1137c478bd9Sstevel@tonic-gate #else
1147c478bd9Sstevel@tonic-gate /*
1157c478bd9Sstevel@tonic-gate * Follow symlinks when traversing directories. Only follow symlinks
1167c478bd9Sstevel@tonic-gate * to other parts of the file hierarchy if -L was specified.
1177c478bd9Sstevel@tonic-gate */
1187c478bd9Sstevel@tonic-gate #define FOLLOW_D_LINKS (Lflag)
1197c478bd9Sstevel@tonic-gate #endif
1207c478bd9Sstevel@tonic-gate
1217c478bd9Sstevel@tonic-gate #define CHOWN(f, u, g) if (chown(f, u, g) < 0) { \
1227c478bd9Sstevel@tonic-gate status += Perror(f); \
1237c478bd9Sstevel@tonic-gate }
1247c478bd9Sstevel@tonic-gate
1257c478bd9Sstevel@tonic-gate #define LCHOWN(f, u, g) if (lchown(f, u, g) < 0) { \
1267c478bd9Sstevel@tonic-gate status += Perror(f); \
1277c478bd9Sstevel@tonic-gate }
1287c478bd9Sstevel@tonic-gate /*
1297c478bd9Sstevel@tonic-gate * We're ignoring errors here because preserving the SET[UG]ID bits is just
1307c478bd9Sstevel@tonic-gate * a courtesy. This is only used on directories.
1317c478bd9Sstevel@tonic-gate */
1327c478bd9Sstevel@tonic-gate #define SETUGID_PRESERVE(dir, mode) \
1337c478bd9Sstevel@tonic-gate if (((mode) & (S_ISGID|S_ISUID)) != 0) \
1347c478bd9Sstevel@tonic-gate (void) chmod((dir), (mode) & ~S_IFMT)
1357c478bd9Sstevel@tonic-gate
1367c478bd9Sstevel@tonic-gate extern int optind;
1377c478bd9Sstevel@tonic-gate
1387c478bd9Sstevel@tonic-gate
1397c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])1407c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
1417c478bd9Sstevel@tonic-gate {
1427c478bd9Sstevel@tonic-gate int c;
1437c478bd9Sstevel@tonic-gate
1447c478bd9Sstevel@tonic-gate /* set the locale for only the messages system (all else is clean) */
1457c478bd9Sstevel@tonic-gate
1467c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
1477c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
1487c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */
1497c478bd9Sstevel@tonic-gate #endif
1507c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
1517c478bd9Sstevel@tonic-gate
152*b249c65cSmarks while ((c = getopt(argc, argv, "RhfHLPs")) != EOF)
1537c478bd9Sstevel@tonic-gate switch (c) {
1547c478bd9Sstevel@tonic-gate case 'R':
1557c478bd9Sstevel@tonic-gate rflag++;
1567c478bd9Sstevel@tonic-gate break;
1577c478bd9Sstevel@tonic-gate case 'h':
1587c478bd9Sstevel@tonic-gate hflag++;
1597c478bd9Sstevel@tonic-gate break;
1607c478bd9Sstevel@tonic-gate case 'f':
1617c478bd9Sstevel@tonic-gate fflag++;
1627c478bd9Sstevel@tonic-gate break;
1637c478bd9Sstevel@tonic-gate case 'H':
1647c478bd9Sstevel@tonic-gate /*
1657c478bd9Sstevel@tonic-gate * If more than one of -H, -L, and -P
1667c478bd9Sstevel@tonic-gate * are specified, only the last option
1677c478bd9Sstevel@tonic-gate * specified determines the behavior of
1687c478bd9Sstevel@tonic-gate * chgrp. In addition, make [-H|-L]
1697c478bd9Sstevel@tonic-gate * mutually exclusive of -h.
1707c478bd9Sstevel@tonic-gate */
1717c478bd9Sstevel@tonic-gate Lflag = Pflag = 0;
1727c478bd9Sstevel@tonic-gate Hflag++;
1737c478bd9Sstevel@tonic-gate break;
1747c478bd9Sstevel@tonic-gate case 'L':
1757c478bd9Sstevel@tonic-gate Hflag = Pflag = 0;
1767c478bd9Sstevel@tonic-gate Lflag++;
1777c478bd9Sstevel@tonic-gate break;
1787c478bd9Sstevel@tonic-gate case 'P':
1797c478bd9Sstevel@tonic-gate Hflag = Lflag = 0;
1807c478bd9Sstevel@tonic-gate Pflag++;
1817c478bd9Sstevel@tonic-gate break;
182*b249c65cSmarks case 's':
183*b249c65cSmarks sflag++;
184*b249c65cSmarks break;
1857c478bd9Sstevel@tonic-gate default:
1867c478bd9Sstevel@tonic-gate usage();
1877c478bd9Sstevel@tonic-gate }
1887c478bd9Sstevel@tonic-gate
1897c478bd9Sstevel@tonic-gate /*
1907c478bd9Sstevel@tonic-gate * Check for sufficient arguments
1917c478bd9Sstevel@tonic-gate * or a usage error.
1927c478bd9Sstevel@tonic-gate */
1937c478bd9Sstevel@tonic-gate argc -= optind;
1947c478bd9Sstevel@tonic-gate argv = &argv[optind];
1957c478bd9Sstevel@tonic-gate
1967c478bd9Sstevel@tonic-gate if ((argc < 2) ||
1977c478bd9Sstevel@tonic-gate ((Hflag || Lflag || Pflag) && !rflag) ||
1987c478bd9Sstevel@tonic-gate ((Hflag || Lflag || Pflag) && hflag)) {
1997c478bd9Sstevel@tonic-gate usage();
2007c478bd9Sstevel@tonic-gate }
2017c478bd9Sstevel@tonic-gate
202*b249c65cSmarks if (sflag) {
203*b249c65cSmarks if (sid_to_id(argv[0], B_FALSE, &gid)) {
204*b249c65cSmarks (void) fprintf(stderr, gettext(
205*b249c65cSmarks "chgrp: invalid group sid %s\n"), argv[0]);
206*b249c65cSmarks exit(2);
207*b249c65cSmarks }
208*b249c65cSmarks } else if ((gr = getgrnam(argv[0])) != NULL) {
209bda8495cSas145665 gid = gr->gr_gid;
210bda8495cSas145665 } else {
2117c478bd9Sstevel@tonic-gate if (isnumber(argv[0])) {
2127c478bd9Sstevel@tonic-gate errno = 0;
213bda8495cSas145665 /* gid is an int */
214*b249c65cSmarks gid = (gid_t)strtoul(argv[0], NULL, 10);
2157c478bd9Sstevel@tonic-gate if (errno != 0) {
2167c478bd9Sstevel@tonic-gate if (errno == ERANGE) {
2177c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(
2187c478bd9Sstevel@tonic-gate "chgrp: group id is too large\n"));
2197c478bd9Sstevel@tonic-gate exit(2);
2207c478bd9Sstevel@tonic-gate } else {
2217c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(
2227c478bd9Sstevel@tonic-gate "chgrp: invalid group id\n"));
2237c478bd9Sstevel@tonic-gate exit(2);
2247c478bd9Sstevel@tonic-gate }
2257c478bd9Sstevel@tonic-gate }
2267c478bd9Sstevel@tonic-gate } else {
2277c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "chgrp: ");
2287c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("unknown group: %s\n"),
2297c478bd9Sstevel@tonic-gate argv[0]);
2307c478bd9Sstevel@tonic-gate exit(2);
2317c478bd9Sstevel@tonic-gate }
2327c478bd9Sstevel@tonic-gate }
2337c478bd9Sstevel@tonic-gate
2347c478bd9Sstevel@tonic-gate for (c = 1; c < argc; c++) {
2357c478bd9Sstevel@tonic-gate tree = NULL;
2367c478bd9Sstevel@tonic-gate if (lstat(argv[c], &stbuf) < 0) {
2377c478bd9Sstevel@tonic-gate status += Perror(argv[c]);
2387c478bd9Sstevel@tonic-gate continue;
2397c478bd9Sstevel@tonic-gate }
2407c478bd9Sstevel@tonic-gate if (rflag && ((stbuf.st_mode & S_IFMT) == S_IFLNK)) {
2417c478bd9Sstevel@tonic-gate if (hflag || Pflag) {
2427c478bd9Sstevel@tonic-gate /*
2437c478bd9Sstevel@tonic-gate * Change the group id of the symbolic link
2447c478bd9Sstevel@tonic-gate * specified on the command line.
2457c478bd9Sstevel@tonic-gate * Don't follow the symbolic link to
2467c478bd9Sstevel@tonic-gate * any other part of the file hierarchy.
2477c478bd9Sstevel@tonic-gate */
2487c478bd9Sstevel@tonic-gate LCHOWN(argv[c], -1, gid);
2497c478bd9Sstevel@tonic-gate } else {
2507c478bd9Sstevel@tonic-gate if (stat(argv[c], &stbuf2) < 0) {
2517c478bd9Sstevel@tonic-gate status += Perror(argv[c]);
2527c478bd9Sstevel@tonic-gate continue;
2537c478bd9Sstevel@tonic-gate }
2547c478bd9Sstevel@tonic-gate /*
2557c478bd9Sstevel@tonic-gate * We know that we are to change the
2567c478bd9Sstevel@tonic-gate * group of the file referenced by the
2577c478bd9Sstevel@tonic-gate * symlink specified on the command line.
2587c478bd9Sstevel@tonic-gate * Now check to see if we are to follow
2597c478bd9Sstevel@tonic-gate * the symlink to any other part of the
2607c478bd9Sstevel@tonic-gate * file hierarchy.
2617c478bd9Sstevel@tonic-gate */
2627c478bd9Sstevel@tonic-gate if (FOLLOW_CL_LINKS) {
2637c478bd9Sstevel@tonic-gate if ((stbuf2.st_mode & S_IFMT)
2647c478bd9Sstevel@tonic-gate == S_IFDIR) {
2657c478bd9Sstevel@tonic-gate /*
2667c478bd9Sstevel@tonic-gate * We are following symlinks so
2677c478bd9Sstevel@tonic-gate * traverse into the directory.
2687c478bd9Sstevel@tonic-gate * Add this node to the search
2697c478bd9Sstevel@tonic-gate * tree so we don't get into an
2707c478bd9Sstevel@tonic-gate * endless loop.
2717c478bd9Sstevel@tonic-gate */
2727c478bd9Sstevel@tonic-gate if (add_tnode(&tree,
2737c478bd9Sstevel@tonic-gate stbuf2.st_dev,
2747c478bd9Sstevel@tonic-gate stbuf2.st_ino) == 1) {
2757c478bd9Sstevel@tonic-gate chgrpr(argv[c], gid);
2767c478bd9Sstevel@tonic-gate /*
2777c478bd9Sstevel@tonic-gate * Try to restore the
2787c478bd9Sstevel@tonic-gate * SET[UG]ID bits.
2797c478bd9Sstevel@tonic-gate */
2807c478bd9Sstevel@tonic-gate SETUGID_PRESERVE(
2817c478bd9Sstevel@tonic-gate argv[c],
2827c478bd9Sstevel@tonic-gate stbuf2.st_mode &
2837c478bd9Sstevel@tonic-gate ~S_IFMT);
2847c478bd9Sstevel@tonic-gate } else {
2857c478bd9Sstevel@tonic-gate /*
2867c478bd9Sstevel@tonic-gate * Error occurred.
2877c478bd9Sstevel@tonic-gate * rc can't be 0
2887c478bd9Sstevel@tonic-gate * as this is the first
2897c478bd9Sstevel@tonic-gate * node to be added to
2907c478bd9Sstevel@tonic-gate * the search tree.
2917c478bd9Sstevel@tonic-gate */
2927c478bd9Sstevel@tonic-gate status += Perror(
2937c478bd9Sstevel@tonic-gate argv[c]);
2947c478bd9Sstevel@tonic-gate }
2957c478bd9Sstevel@tonic-gate } else {
2967c478bd9Sstevel@tonic-gate /*
2977c478bd9Sstevel@tonic-gate * Change the group id of the
2987c478bd9Sstevel@tonic-gate * file referenced by the
2997c478bd9Sstevel@tonic-gate * symbolic link.
3007c478bd9Sstevel@tonic-gate */
3017c478bd9Sstevel@tonic-gate CHOWN(argv[c], -1, gid);
3027c478bd9Sstevel@tonic-gate }
3037c478bd9Sstevel@tonic-gate } else {
3047c478bd9Sstevel@tonic-gate /*
3057c478bd9Sstevel@tonic-gate * Change the group id of the file
3067c478bd9Sstevel@tonic-gate * referenced by the symbolic link.
3077c478bd9Sstevel@tonic-gate */
3087c478bd9Sstevel@tonic-gate CHOWN(argv[c], -1, gid);
3097c478bd9Sstevel@tonic-gate
3107c478bd9Sstevel@tonic-gate if ((stbuf2.st_mode & S_IFMT)
3117c478bd9Sstevel@tonic-gate == S_IFDIR) {
3127c478bd9Sstevel@tonic-gate /* Reset the SET[UG]ID bits. */
3137c478bd9Sstevel@tonic-gate SETUGID_PRESERVE(argv[c],
3147c478bd9Sstevel@tonic-gate stbuf2.st_mode & ~S_IFMT);
3157c478bd9Sstevel@tonic-gate }
3167c478bd9Sstevel@tonic-gate }
3177c478bd9Sstevel@tonic-gate }
3187c478bd9Sstevel@tonic-gate } else if (rflag && ((stbuf.st_mode & S_IFMT) == S_IFDIR)) {
3197c478bd9Sstevel@tonic-gate /*
3207c478bd9Sstevel@tonic-gate * Add this node to the search tree so we don't
3217c478bd9Sstevel@tonic-gate * get into a endless loop.
3227c478bd9Sstevel@tonic-gate */
3237c478bd9Sstevel@tonic-gate if (add_tnode(&tree, stbuf.st_dev,
3247c478bd9Sstevel@tonic-gate stbuf.st_ino) == 1) {
3257c478bd9Sstevel@tonic-gate chgrpr(argv[c], gid);
3267c478bd9Sstevel@tonic-gate
3277c478bd9Sstevel@tonic-gate /* Restore the SET[UG]ID bits. */
3287c478bd9Sstevel@tonic-gate SETUGID_PRESERVE(argv[c],
3297c478bd9Sstevel@tonic-gate stbuf.st_mode & ~S_IFMT);
3307c478bd9Sstevel@tonic-gate } else {
3317c478bd9Sstevel@tonic-gate /*
3327c478bd9Sstevel@tonic-gate * An error occurred while trying
3337c478bd9Sstevel@tonic-gate * to add the node to the tree.
3347c478bd9Sstevel@tonic-gate * Continue on with next file
3357c478bd9Sstevel@tonic-gate * specified. Note: rc shouldn't
3367c478bd9Sstevel@tonic-gate * be 0 as this was the first node
3377c478bd9Sstevel@tonic-gate * being added to the search tree.
3387c478bd9Sstevel@tonic-gate */
3397c478bd9Sstevel@tonic-gate status += Perror(argv[c]);
3407c478bd9Sstevel@tonic-gate }
3417c478bd9Sstevel@tonic-gate } else {
3427c478bd9Sstevel@tonic-gate if (hflag || Pflag) {
3437c478bd9Sstevel@tonic-gate LCHOWN(argv[c], -1, gid);
3447c478bd9Sstevel@tonic-gate } else {
3457c478bd9Sstevel@tonic-gate CHOWN(argv[c], -1, gid);
3467c478bd9Sstevel@tonic-gate }
3477c478bd9Sstevel@tonic-gate /* If a directory, reset the SET[UG]ID bits. */
3487c478bd9Sstevel@tonic-gate if ((stbuf.st_mode & S_IFMT) == S_IFDIR) {
3497c478bd9Sstevel@tonic-gate SETUGID_PRESERVE(argv[c],
3507c478bd9Sstevel@tonic-gate stbuf.st_mode & ~S_IFMT);
3517c478bd9Sstevel@tonic-gate }
3527c478bd9Sstevel@tonic-gate }
3537c478bd9Sstevel@tonic-gate }
3547c478bd9Sstevel@tonic-gate return (status);
3557c478bd9Sstevel@tonic-gate }
3567c478bd9Sstevel@tonic-gate
3577c478bd9Sstevel@tonic-gate /*
3587c478bd9Sstevel@tonic-gate * chgrpr() - recursive chown()
3597c478bd9Sstevel@tonic-gate *
3607c478bd9Sstevel@tonic-gate * Recursively chowns the input directory then its contents. rflag must
3617c478bd9Sstevel@tonic-gate * have been set if chgrpr() is called. The input directory should not
3627c478bd9Sstevel@tonic-gate * be a sym link (this is handled in the calling routine). In
3637c478bd9Sstevel@tonic-gate * addition, the calling routine should have already added the input
3647c478bd9Sstevel@tonic-gate * directory to the search tree so we do not get into endless loops.
3657c478bd9Sstevel@tonic-gate * Note: chgrpr() doesn't need a return value as errors are reported
3667c478bd9Sstevel@tonic-gate * through the global "status" variable.
3677c478bd9Sstevel@tonic-gate */
3687c478bd9Sstevel@tonic-gate static void
chgrpr(char * dir,gid_t gid)3697c478bd9Sstevel@tonic-gate chgrpr(char *dir, gid_t gid)
3707c478bd9Sstevel@tonic-gate {
3717c478bd9Sstevel@tonic-gate struct dirent *dp;
3727c478bd9Sstevel@tonic-gate DIR *dirp;
3737c478bd9Sstevel@tonic-gate struct stat st, st2;
3747c478bd9Sstevel@tonic-gate char savedir[1024];
3757c478bd9Sstevel@tonic-gate
3767c478bd9Sstevel@tonic-gate if (getcwd(savedir, 1024) == 0) {
3777c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "chgrp: ");
3787c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s\n"), savedir);
3797c478bd9Sstevel@tonic-gate exit(255);
3807c478bd9Sstevel@tonic-gate }
3817c478bd9Sstevel@tonic-gate
3827c478bd9Sstevel@tonic-gate /*
3837c478bd9Sstevel@tonic-gate * Attempt to chown the directory, however don't return if we
3847c478bd9Sstevel@tonic-gate * can't as we still may be able to chown the contents of the
3857c478bd9Sstevel@tonic-gate * directory. Note: the calling routine resets the SUID bits
3867c478bd9Sstevel@tonic-gate * on this directory so we don't have to perform an extra 'stat'.
3877c478bd9Sstevel@tonic-gate */
3887c478bd9Sstevel@tonic-gate CHOWN(dir, -1, gid);
3897c478bd9Sstevel@tonic-gate
3907c478bd9Sstevel@tonic-gate if (chdir(dir) < 0) {
3917c478bd9Sstevel@tonic-gate status += Perror(dir);
3927c478bd9Sstevel@tonic-gate return;
3937c478bd9Sstevel@tonic-gate }
3947c478bd9Sstevel@tonic-gate if ((dirp = opendir(".")) == NULL) {
3957c478bd9Sstevel@tonic-gate status += Perror(dir);
3967c478bd9Sstevel@tonic-gate return;
3977c478bd9Sstevel@tonic-gate }
3987c478bd9Sstevel@tonic-gate for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) {
3997c478bd9Sstevel@tonic-gate if ((strcmp(dp->d_name, ".") == 0) ||
4007c478bd9Sstevel@tonic-gate (strcmp(dp->d_name, "..") == 0)) {
4017c478bd9Sstevel@tonic-gate continue; /* skip "." and ".." */
4027c478bd9Sstevel@tonic-gate }
4037c478bd9Sstevel@tonic-gate if (lstat(dp->d_name, &st) < 0) {
4047c478bd9Sstevel@tonic-gate status += Perror(dp->d_name);
4057c478bd9Sstevel@tonic-gate continue;
4067c478bd9Sstevel@tonic-gate }
4077c478bd9Sstevel@tonic-gate if ((st.st_mode & S_IFMT) == S_IFLNK) {
4087c478bd9Sstevel@tonic-gate if (hflag || Pflag) {
4097c478bd9Sstevel@tonic-gate /*
4107c478bd9Sstevel@tonic-gate * Change the group id of the symbolic link
4117c478bd9Sstevel@tonic-gate * encountered while traversing the
4127c478bd9Sstevel@tonic-gate * directory. Don't follow the symbolic
4137c478bd9Sstevel@tonic-gate * link to any other part of the file
4147c478bd9Sstevel@tonic-gate * hierarchy.
4157c478bd9Sstevel@tonic-gate */
4167c478bd9Sstevel@tonic-gate LCHOWN(dp->d_name, -1, gid);
4177c478bd9Sstevel@tonic-gate } else {
4187c478bd9Sstevel@tonic-gate if (stat(dp->d_name, &st2) < 0) {
4197c478bd9Sstevel@tonic-gate status += Perror(dp->d_name);
4207c478bd9Sstevel@tonic-gate continue;
4217c478bd9Sstevel@tonic-gate }
4227c478bd9Sstevel@tonic-gate /*
4237c478bd9Sstevel@tonic-gate * We know that we are to change the
4247c478bd9Sstevel@tonic-gate * group of the file referenced by the
4257c478bd9Sstevel@tonic-gate * symlink encountered while traversing
4267c478bd9Sstevel@tonic-gate * the directory. Now check to see if we
4277c478bd9Sstevel@tonic-gate * are to follow the symlink to any other
4287c478bd9Sstevel@tonic-gate * part of the file hierarchy.
4297c478bd9Sstevel@tonic-gate */
4307c478bd9Sstevel@tonic-gate if (FOLLOW_D_LINKS) {
4317c478bd9Sstevel@tonic-gate if ((st2.st_mode & S_IFMT) == S_IFDIR) {
4327c478bd9Sstevel@tonic-gate /*
4337c478bd9Sstevel@tonic-gate * We are following symlinks so
4347c478bd9Sstevel@tonic-gate * traverse into the directory.
4357c478bd9Sstevel@tonic-gate * Add this node to the search
4367c478bd9Sstevel@tonic-gate * tree so we don't get into an
4377c478bd9Sstevel@tonic-gate * endless loop.
4387c478bd9Sstevel@tonic-gate */
4397c478bd9Sstevel@tonic-gate int rc;
4407c478bd9Sstevel@tonic-gate if ((rc = add_tnode(&tree,
4417c478bd9Sstevel@tonic-gate st2.st_dev,
4427c478bd9Sstevel@tonic-gate st2.st_ino)) == 1) {
4437c478bd9Sstevel@tonic-gate chgrpr(dp->d_name, gid);
4447c478bd9Sstevel@tonic-gate
4457c478bd9Sstevel@tonic-gate /*
4467c478bd9Sstevel@tonic-gate * Restore SET[UG]ID
4477c478bd9Sstevel@tonic-gate * bits.
4487c478bd9Sstevel@tonic-gate */
4497c478bd9Sstevel@tonic-gate SETUGID_PRESERVE(
4507c478bd9Sstevel@tonic-gate dp->d_name,
4517c478bd9Sstevel@tonic-gate st2.st_mode &
4527c478bd9Sstevel@tonic-gate ~S_IFMT);
4537c478bd9Sstevel@tonic-gate } else if (rc == 0) {
4547c478bd9Sstevel@tonic-gate /* already visited */
4557c478bd9Sstevel@tonic-gate continue;
4567c478bd9Sstevel@tonic-gate } else {
4577c478bd9Sstevel@tonic-gate /*
4587c478bd9Sstevel@tonic-gate * An error occurred
4597c478bd9Sstevel@tonic-gate * while trying to add
4607c478bd9Sstevel@tonic-gate * the node to the tree.
4617c478bd9Sstevel@tonic-gate */
4627c478bd9Sstevel@tonic-gate status += Perror(
4637c478bd9Sstevel@tonic-gate dp->d_name);
4647c478bd9Sstevel@tonic-gate continue;
4657c478bd9Sstevel@tonic-gate }
4667c478bd9Sstevel@tonic-gate } else {
4677c478bd9Sstevel@tonic-gate /*
4687c478bd9Sstevel@tonic-gate * Change the group id of the
4697c478bd9Sstevel@tonic-gate * file referenced by the
4707c478bd9Sstevel@tonic-gate * symbolic link.
4717c478bd9Sstevel@tonic-gate */
4727c478bd9Sstevel@tonic-gate CHOWN(dp->d_name, -1, gid);
4737c478bd9Sstevel@tonic-gate
4747c478bd9Sstevel@tonic-gate }
4757c478bd9Sstevel@tonic-gate } else {
4767c478bd9Sstevel@tonic-gate /*
4777c478bd9Sstevel@tonic-gate * Change the group id of the file
4787c478bd9Sstevel@tonic-gate * referenced by the symbolic link.
4797c478bd9Sstevel@tonic-gate */
4807c478bd9Sstevel@tonic-gate CHOWN(dp->d_name, -1, gid);
4817c478bd9Sstevel@tonic-gate
4827c478bd9Sstevel@tonic-gate if ((st2.st_mode & S_IFMT) == S_IFDIR) {
4837c478bd9Sstevel@tonic-gate /* Restore SET[UG]ID bits. */
4847c478bd9Sstevel@tonic-gate SETUGID_PRESERVE(dp->d_name,
4857c478bd9Sstevel@tonic-gate st2.st_mode & ~S_IFMT);
4867c478bd9Sstevel@tonic-gate }
4877c478bd9Sstevel@tonic-gate }
4887c478bd9Sstevel@tonic-gate }
4897c478bd9Sstevel@tonic-gate } else if ((st.st_mode & S_IFMT) == S_IFDIR) {
4907c478bd9Sstevel@tonic-gate /*
4917c478bd9Sstevel@tonic-gate * Add this node to the search tree so we don't
4927c478bd9Sstevel@tonic-gate * get into a endless loop.
4937c478bd9Sstevel@tonic-gate */
4947c478bd9Sstevel@tonic-gate int rc;
4957c478bd9Sstevel@tonic-gate if ((rc = add_tnode(&tree, st.st_dev,
4967c478bd9Sstevel@tonic-gate st.st_ino)) == 1) {
4977c478bd9Sstevel@tonic-gate chgrpr(dp->d_name, gid);
4987c478bd9Sstevel@tonic-gate
4997c478bd9Sstevel@tonic-gate /* Restore the SET[UG]ID bits. */
5007c478bd9Sstevel@tonic-gate SETUGID_PRESERVE(dp->d_name,
5017c478bd9Sstevel@tonic-gate st.st_mode & ~S_IFMT);
5027c478bd9Sstevel@tonic-gate } else if (rc == 0) {
5037c478bd9Sstevel@tonic-gate /* already visited */
5047c478bd9Sstevel@tonic-gate continue;
5057c478bd9Sstevel@tonic-gate } else {
5067c478bd9Sstevel@tonic-gate /*
5077c478bd9Sstevel@tonic-gate * An error occurred while trying
5087c478bd9Sstevel@tonic-gate * to add the node to the search tree.
5097c478bd9Sstevel@tonic-gate */
5107c478bd9Sstevel@tonic-gate status += Perror(dp->d_name);
5117c478bd9Sstevel@tonic-gate continue;
5127c478bd9Sstevel@tonic-gate }
5137c478bd9Sstevel@tonic-gate } else {
5147c478bd9Sstevel@tonic-gate CHOWN(dp->d_name, -1, gid);
5157c478bd9Sstevel@tonic-gate }
5167c478bd9Sstevel@tonic-gate }
5177c478bd9Sstevel@tonic-gate (void) closedir(dirp);
5187c478bd9Sstevel@tonic-gate if (chdir(savedir) < 0) {
5197c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "chgrp: ");
5207c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("can't change back to %s\n"),
5217c478bd9Sstevel@tonic-gate savedir);
5227c478bd9Sstevel@tonic-gate exit(255);
5237c478bd9Sstevel@tonic-gate }
5247c478bd9Sstevel@tonic-gate }
5257c478bd9Sstevel@tonic-gate
5267c478bd9Sstevel@tonic-gate static int
isnumber(char * s)5277c478bd9Sstevel@tonic-gate isnumber(char *s)
5287c478bd9Sstevel@tonic-gate {
5297c478bd9Sstevel@tonic-gate int c;
5307c478bd9Sstevel@tonic-gate
5317c478bd9Sstevel@tonic-gate while ((c = *s++) != '\0')
5327c478bd9Sstevel@tonic-gate if (!isdigit(c))
5337c478bd9Sstevel@tonic-gate return (0);
5347c478bd9Sstevel@tonic-gate return (1);
5357c478bd9Sstevel@tonic-gate }
5367c478bd9Sstevel@tonic-gate
5377c478bd9Sstevel@tonic-gate
5387c478bd9Sstevel@tonic-gate static int
Perror(char * s)5397c478bd9Sstevel@tonic-gate Perror(char *s)
5407c478bd9Sstevel@tonic-gate {
5417c478bd9Sstevel@tonic-gate if (!fflag) {
5427c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "chgrp: ");
5437c478bd9Sstevel@tonic-gate perror(s);
5447c478bd9Sstevel@tonic-gate }
5457c478bd9Sstevel@tonic-gate return (!fflag);
5467c478bd9Sstevel@tonic-gate }
5477c478bd9Sstevel@tonic-gate
5487c478bd9Sstevel@tonic-gate
5497c478bd9Sstevel@tonic-gate static void
usage(void)5507c478bd9Sstevel@tonic-gate usage(void)
5517c478bd9Sstevel@tonic-gate {
5527c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(
5537c478bd9Sstevel@tonic-gate "usage:\n"
5547c478bd9Sstevel@tonic-gate "\tchgrp [-fhR] group file ...\n"
555*b249c65cSmarks "\tchgrp -R [-f] [-H|-L|-P] group file ...\n"
556*b249c65cSmarks "\tchgrp -s [-fhR] groupsid file ...\n"
557*b249c65cSmarks "\tchgrp -s -R [-f] [-H|-L|-P] groupsid file ...\n"));
5587c478bd9Sstevel@tonic-gate exit(2);
5597c478bd9Sstevel@tonic-gate }
560