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 /*
22b249c65cSmarks * 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 /*
427c478bd9Sstevel@tonic-gate * chgrp [-fhR] gid file ...
437c478bd9Sstevel@tonic-gate * chgrp -R [-f] [-H|-L|-P] gid file ...
44b249c65cSmarks * chgrp -s [-fhR] groupsid file ...
45b249c65cSmarks * chgrp -s -R [-f] [-H|-L|-P] groupsid file ...
467c478bd9Sstevel@tonic-gate */
477c478bd9Sstevel@tonic-gate
487c478bd9Sstevel@tonic-gate #include <stdio.h>
497c478bd9Sstevel@tonic-gate #include <ctype.h>
507c478bd9Sstevel@tonic-gate #include <sys/types.h>
517c478bd9Sstevel@tonic-gate #include <sys/stat.h>
527c478bd9Sstevel@tonic-gate #include <sys/avl.h>
537c478bd9Sstevel@tonic-gate #include <grp.h>
547c478bd9Sstevel@tonic-gate #include <dirent.h>
557c478bd9Sstevel@tonic-gate #include <unistd.h>
567c478bd9Sstevel@tonic-gate #include <stdlib.h>
577c478bd9Sstevel@tonic-gate #include <locale.h>
587c478bd9Sstevel@tonic-gate #include <libcmdutils.h>
597c478bd9Sstevel@tonic-gate #include <errno.h>
607c478bd9Sstevel@tonic-gate #include <strings.h>
61b249c65cSmarks #include <aclutils.h>
627c478bd9Sstevel@tonic-gate
637c478bd9Sstevel@tonic-gate static struct group *gr;
647c478bd9Sstevel@tonic-gate static struct stat stbuf;
657c478bd9Sstevel@tonic-gate static struct stat stbuf2;
667c478bd9Sstevel@tonic-gate static gid_t gid;
677c478bd9Sstevel@tonic-gate static int hflag = 0,
687c478bd9Sstevel@tonic-gate fflag = 0,
697c478bd9Sstevel@tonic-gate rflag = 0,
707c478bd9Sstevel@tonic-gate Hflag = 0,
717c478bd9Sstevel@tonic-gate Lflag = 0,
72b249c65cSmarks Pflag = 0,
73b249c65cSmarks sflag = 0;
747c478bd9Sstevel@tonic-gate static int status = 0; /* total number of errors received */
757c478bd9Sstevel@tonic-gate
767c478bd9Sstevel@tonic-gate static avl_tree_t *tree; /* search tree to store inode data */
777c478bd9Sstevel@tonic-gate
787c478bd9Sstevel@tonic-gate static void usage(void);
797c478bd9Sstevel@tonic-gate static int isnumber(char *);
807c478bd9Sstevel@tonic-gate static int Perror(char *);
817c478bd9Sstevel@tonic-gate static void chgrpr(char *, gid_t);
827c478bd9Sstevel@tonic-gate
837c478bd9Sstevel@tonic-gate /*
847c478bd9Sstevel@tonic-gate * Check to see if we are to follow symlinks specified on the command line.
857c478bd9Sstevel@tonic-gate * This assumes we've already checked to make sure neither -h or -P was
867c478bd9Sstevel@tonic-gate * specified, so we are just looking to see if -R -L, or -R -H was specified.
877c478bd9Sstevel@tonic-gate */
887c478bd9Sstevel@tonic-gate #define FOLLOW_CL_LINKS (rflag && (Hflag || Lflag))
897c478bd9Sstevel@tonic-gate
907c478bd9Sstevel@tonic-gate /*
917c478bd9Sstevel@tonic-gate * Follow symlinks when traversing directories. Only follow symlinks
927c478bd9Sstevel@tonic-gate * to other parts of the file hierarchy if -L was specified.
937c478bd9Sstevel@tonic-gate */
947c478bd9Sstevel@tonic-gate #define FOLLOW_D_LINKS (Lflag)
957c478bd9Sstevel@tonic-gate
967c478bd9Sstevel@tonic-gate #define CHOWN(f, u, g) if (chown(f, u, g) < 0) { \
977c478bd9Sstevel@tonic-gate status += Perror(f); \
987c478bd9Sstevel@tonic-gate }
997c478bd9Sstevel@tonic-gate
1007c478bd9Sstevel@tonic-gate #define LCHOWN(f, u, g) if (lchown(f, u, g) < 0) { \
1017c478bd9Sstevel@tonic-gate status += Perror(f); \
1027c478bd9Sstevel@tonic-gate }
1037c478bd9Sstevel@tonic-gate /*
1047c478bd9Sstevel@tonic-gate * We're ignoring errors here because preserving the SET[UG]ID bits is just
1057c478bd9Sstevel@tonic-gate * a courtesy. This is only used on directories.
1067c478bd9Sstevel@tonic-gate */
1077c478bd9Sstevel@tonic-gate #define SETUGID_PRESERVE(dir, mode) \
1087c478bd9Sstevel@tonic-gate if (((mode) & (S_ISGID|S_ISUID)) != 0) \
1097c478bd9Sstevel@tonic-gate (void) chmod((dir), (mode) & ~S_IFMT)
1107c478bd9Sstevel@tonic-gate
1117c478bd9Sstevel@tonic-gate extern int optind;
1127c478bd9Sstevel@tonic-gate
1137c478bd9Sstevel@tonic-gate
1147c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])1157c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
1167c478bd9Sstevel@tonic-gate {
1177c478bd9Sstevel@tonic-gate int c;
1187c478bd9Sstevel@tonic-gate
1197c478bd9Sstevel@tonic-gate /* set the locale for only the messages system (all else is clean) */
1207c478bd9Sstevel@tonic-gate
1217c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
1227c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
1237c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */
1247c478bd9Sstevel@tonic-gate #endif
1257c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
1267c478bd9Sstevel@tonic-gate
127b249c65cSmarks while ((c = getopt(argc, argv, "RhfHLPs")) != EOF)
1287c478bd9Sstevel@tonic-gate switch (c) {
1297c478bd9Sstevel@tonic-gate case 'R':
1307c478bd9Sstevel@tonic-gate rflag++;
1317c478bd9Sstevel@tonic-gate break;
1327c478bd9Sstevel@tonic-gate case 'h':
1337c478bd9Sstevel@tonic-gate hflag++;
1347c478bd9Sstevel@tonic-gate break;
1357c478bd9Sstevel@tonic-gate case 'f':
1367c478bd9Sstevel@tonic-gate fflag++;
1377c478bd9Sstevel@tonic-gate break;
1387c478bd9Sstevel@tonic-gate case 'H':
1397c478bd9Sstevel@tonic-gate /*
1407c478bd9Sstevel@tonic-gate * If more than one of -H, -L, and -P
1417c478bd9Sstevel@tonic-gate * are specified, only the last option
1427c478bd9Sstevel@tonic-gate * specified determines the behavior of
1437c478bd9Sstevel@tonic-gate * chgrp. In addition, make [-H|-L]
1447c478bd9Sstevel@tonic-gate * mutually exclusive of -h.
1457c478bd9Sstevel@tonic-gate */
1467c478bd9Sstevel@tonic-gate Lflag = Pflag = 0;
1477c478bd9Sstevel@tonic-gate Hflag++;
1487c478bd9Sstevel@tonic-gate break;
1497c478bd9Sstevel@tonic-gate case 'L':
1507c478bd9Sstevel@tonic-gate Hflag = Pflag = 0;
1517c478bd9Sstevel@tonic-gate Lflag++;
1527c478bd9Sstevel@tonic-gate break;
1537c478bd9Sstevel@tonic-gate case 'P':
1547c478bd9Sstevel@tonic-gate Hflag = Lflag = 0;
1557c478bd9Sstevel@tonic-gate Pflag++;
1567c478bd9Sstevel@tonic-gate break;
157b249c65cSmarks case 's':
158b249c65cSmarks sflag++;
159b249c65cSmarks break;
1607c478bd9Sstevel@tonic-gate default:
1617c478bd9Sstevel@tonic-gate usage();
1627c478bd9Sstevel@tonic-gate }
163*8f5edf1fSAlexander Pyhalov /*
164*8f5edf1fSAlexander Pyhalov * Set Pflag by default for recursive operations
165*8f5edf1fSAlexander Pyhalov * if no other options were specified.
166*8f5edf1fSAlexander Pyhalov */
167*8f5edf1fSAlexander Pyhalov if (rflag && !(Lflag || Hflag || Pflag || hflag)) {
168*8f5edf1fSAlexander Pyhalov Pflag = 1;
169*8f5edf1fSAlexander Pyhalov }
1707c478bd9Sstevel@tonic-gate
1717c478bd9Sstevel@tonic-gate /*
1727c478bd9Sstevel@tonic-gate * Check for sufficient arguments
1737c478bd9Sstevel@tonic-gate * or a usage error.
1747c478bd9Sstevel@tonic-gate */
1757c478bd9Sstevel@tonic-gate argc -= optind;
1767c478bd9Sstevel@tonic-gate argv = &argv[optind];
1777c478bd9Sstevel@tonic-gate
1787c478bd9Sstevel@tonic-gate if ((argc < 2) ||
1797c478bd9Sstevel@tonic-gate ((Hflag || Lflag || Pflag) && !rflag) ||
1807c478bd9Sstevel@tonic-gate ((Hflag || Lflag || Pflag) && hflag)) {
1817c478bd9Sstevel@tonic-gate usage();
1827c478bd9Sstevel@tonic-gate }
1837c478bd9Sstevel@tonic-gate
184b249c65cSmarks if (sflag) {
185b249c65cSmarks if (sid_to_id(argv[0], B_FALSE, &gid)) {
186b249c65cSmarks (void) fprintf(stderr, gettext(
187b249c65cSmarks "chgrp: invalid group sid %s\n"), argv[0]);
188b249c65cSmarks exit(2);
189b249c65cSmarks }
190b249c65cSmarks } else if ((gr = getgrnam(argv[0])) != NULL) {
191bda8495cSas145665 gid = gr->gr_gid;
192bda8495cSas145665 } else {
1937c478bd9Sstevel@tonic-gate if (isnumber(argv[0])) {
1947c478bd9Sstevel@tonic-gate errno = 0;
195bda8495cSas145665 /* gid is an int */
196b249c65cSmarks gid = (gid_t)strtoul(argv[0], NULL, 10);
1977c478bd9Sstevel@tonic-gate if (errno != 0) {
1987c478bd9Sstevel@tonic-gate if (errno == ERANGE) {
1997c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(
2007c478bd9Sstevel@tonic-gate "chgrp: group id is too large\n"));
2017c478bd9Sstevel@tonic-gate exit(2);
2027c478bd9Sstevel@tonic-gate } else {
2037c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(
2047c478bd9Sstevel@tonic-gate "chgrp: invalid group id\n"));
2057c478bd9Sstevel@tonic-gate exit(2);
2067c478bd9Sstevel@tonic-gate }
2077c478bd9Sstevel@tonic-gate }
2087c478bd9Sstevel@tonic-gate } else {
2097c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "chgrp: ");
2107c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("unknown group: %s\n"),
2117c478bd9Sstevel@tonic-gate argv[0]);
2127c478bd9Sstevel@tonic-gate exit(2);
2137c478bd9Sstevel@tonic-gate }
2147c478bd9Sstevel@tonic-gate }
2157c478bd9Sstevel@tonic-gate
2167c478bd9Sstevel@tonic-gate for (c = 1; c < argc; c++) {
2177c478bd9Sstevel@tonic-gate tree = NULL;
2187c478bd9Sstevel@tonic-gate if (lstat(argv[c], &stbuf) < 0) {
2197c478bd9Sstevel@tonic-gate status += Perror(argv[c]);
2207c478bd9Sstevel@tonic-gate continue;
2217c478bd9Sstevel@tonic-gate }
2227c478bd9Sstevel@tonic-gate if (rflag && ((stbuf.st_mode & S_IFMT) == S_IFLNK)) {
2237c478bd9Sstevel@tonic-gate if (hflag || Pflag) {
2247c478bd9Sstevel@tonic-gate /*
2257c478bd9Sstevel@tonic-gate * Change the group id of the symbolic link
2267c478bd9Sstevel@tonic-gate * specified on the command line.
2277c478bd9Sstevel@tonic-gate * Don't follow the symbolic link to
2287c478bd9Sstevel@tonic-gate * any other part of the file hierarchy.
2297c478bd9Sstevel@tonic-gate */
2307c478bd9Sstevel@tonic-gate LCHOWN(argv[c], -1, gid);
2317c478bd9Sstevel@tonic-gate } else {
2327c478bd9Sstevel@tonic-gate if (stat(argv[c], &stbuf2) < 0) {
2337c478bd9Sstevel@tonic-gate status += Perror(argv[c]);
2347c478bd9Sstevel@tonic-gate continue;
2357c478bd9Sstevel@tonic-gate }
2367c478bd9Sstevel@tonic-gate /*
2377c478bd9Sstevel@tonic-gate * We know that we are to change the
2387c478bd9Sstevel@tonic-gate * group of the file referenced by the
2397c478bd9Sstevel@tonic-gate * symlink specified on the command line.
2407c478bd9Sstevel@tonic-gate * Now check to see if we are to follow
2417c478bd9Sstevel@tonic-gate * the symlink to any other part of the
2427c478bd9Sstevel@tonic-gate * file hierarchy.
2437c478bd9Sstevel@tonic-gate */
2447c478bd9Sstevel@tonic-gate if (FOLLOW_CL_LINKS) {
2457c478bd9Sstevel@tonic-gate if ((stbuf2.st_mode & S_IFMT)
2467c478bd9Sstevel@tonic-gate == S_IFDIR) {
2477c478bd9Sstevel@tonic-gate /*
2487c478bd9Sstevel@tonic-gate * We are following symlinks so
2497c478bd9Sstevel@tonic-gate * traverse into the directory.
2507c478bd9Sstevel@tonic-gate * Add this node to the search
2517c478bd9Sstevel@tonic-gate * tree so we don't get into an
2527c478bd9Sstevel@tonic-gate * endless loop.
2537c478bd9Sstevel@tonic-gate */
2547c478bd9Sstevel@tonic-gate if (add_tnode(&tree,
2557c478bd9Sstevel@tonic-gate stbuf2.st_dev,
2567c478bd9Sstevel@tonic-gate stbuf2.st_ino) == 1) {
2577c478bd9Sstevel@tonic-gate chgrpr(argv[c], gid);
2587c478bd9Sstevel@tonic-gate /*
2597c478bd9Sstevel@tonic-gate * Try to restore the
2607c478bd9Sstevel@tonic-gate * SET[UG]ID bits.
2617c478bd9Sstevel@tonic-gate */
2627c478bd9Sstevel@tonic-gate SETUGID_PRESERVE(
2637c478bd9Sstevel@tonic-gate argv[c],
2647c478bd9Sstevel@tonic-gate stbuf2.st_mode &
2657c478bd9Sstevel@tonic-gate ~S_IFMT);
2667c478bd9Sstevel@tonic-gate } else {
2677c478bd9Sstevel@tonic-gate /*
2687c478bd9Sstevel@tonic-gate * Error occurred.
2697c478bd9Sstevel@tonic-gate * rc can't be 0
2707c478bd9Sstevel@tonic-gate * as this is the first
2717c478bd9Sstevel@tonic-gate * node to be added to
2727c478bd9Sstevel@tonic-gate * the search tree.
2737c478bd9Sstevel@tonic-gate */
2747c478bd9Sstevel@tonic-gate status += Perror(
2757c478bd9Sstevel@tonic-gate argv[c]);
2767c478bd9Sstevel@tonic-gate }
2777c478bd9Sstevel@tonic-gate } else {
2787c478bd9Sstevel@tonic-gate /*
2797c478bd9Sstevel@tonic-gate * Change the group id of the
2807c478bd9Sstevel@tonic-gate * file referenced by the
2817c478bd9Sstevel@tonic-gate * symbolic link.
2827c478bd9Sstevel@tonic-gate */
2837c478bd9Sstevel@tonic-gate CHOWN(argv[c], -1, gid);
2847c478bd9Sstevel@tonic-gate }
2857c478bd9Sstevel@tonic-gate } else {
2867c478bd9Sstevel@tonic-gate /*
2877c478bd9Sstevel@tonic-gate * Change the group id of the file
2887c478bd9Sstevel@tonic-gate * referenced by the symbolic link.
2897c478bd9Sstevel@tonic-gate */
2907c478bd9Sstevel@tonic-gate CHOWN(argv[c], -1, gid);
2917c478bd9Sstevel@tonic-gate
2927c478bd9Sstevel@tonic-gate if ((stbuf2.st_mode & S_IFMT)
2937c478bd9Sstevel@tonic-gate == S_IFDIR) {
2947c478bd9Sstevel@tonic-gate /* Reset the SET[UG]ID bits. */
2957c478bd9Sstevel@tonic-gate SETUGID_PRESERVE(argv[c],
2967c478bd9Sstevel@tonic-gate stbuf2.st_mode & ~S_IFMT);
2977c478bd9Sstevel@tonic-gate }
2987c478bd9Sstevel@tonic-gate }
2997c478bd9Sstevel@tonic-gate }
3007c478bd9Sstevel@tonic-gate } else if (rflag && ((stbuf.st_mode & S_IFMT) == S_IFDIR)) {
3017c478bd9Sstevel@tonic-gate /*
3027c478bd9Sstevel@tonic-gate * Add this node to the search tree so we don't
3037c478bd9Sstevel@tonic-gate * get into a endless loop.
3047c478bd9Sstevel@tonic-gate */
3057c478bd9Sstevel@tonic-gate if (add_tnode(&tree, stbuf.st_dev,
3067c478bd9Sstevel@tonic-gate stbuf.st_ino) == 1) {
3077c478bd9Sstevel@tonic-gate chgrpr(argv[c], gid);
3087c478bd9Sstevel@tonic-gate
3097c478bd9Sstevel@tonic-gate /* Restore the SET[UG]ID bits. */
3107c478bd9Sstevel@tonic-gate SETUGID_PRESERVE(argv[c],
3117c478bd9Sstevel@tonic-gate stbuf.st_mode & ~S_IFMT);
3127c478bd9Sstevel@tonic-gate } else {
3137c478bd9Sstevel@tonic-gate /*
3147c478bd9Sstevel@tonic-gate * An error occurred while trying
3157c478bd9Sstevel@tonic-gate * to add the node to the tree.
3167c478bd9Sstevel@tonic-gate * Continue on with next file
3177c478bd9Sstevel@tonic-gate * specified. Note: rc shouldn't
3187c478bd9Sstevel@tonic-gate * be 0 as this was the first node
3197c478bd9Sstevel@tonic-gate * being added to the search tree.
3207c478bd9Sstevel@tonic-gate */
3217c478bd9Sstevel@tonic-gate status += Perror(argv[c]);
3227c478bd9Sstevel@tonic-gate }
3237c478bd9Sstevel@tonic-gate } else {
3247c478bd9Sstevel@tonic-gate if (hflag || Pflag) {
3257c478bd9Sstevel@tonic-gate LCHOWN(argv[c], -1, gid);
3267c478bd9Sstevel@tonic-gate } else {
3277c478bd9Sstevel@tonic-gate CHOWN(argv[c], -1, gid);
3287c478bd9Sstevel@tonic-gate }
3297c478bd9Sstevel@tonic-gate /* If a directory, reset the SET[UG]ID bits. */
3307c478bd9Sstevel@tonic-gate if ((stbuf.st_mode & S_IFMT) == S_IFDIR) {
3317c478bd9Sstevel@tonic-gate SETUGID_PRESERVE(argv[c],
3327c478bd9Sstevel@tonic-gate stbuf.st_mode & ~S_IFMT);
3337c478bd9Sstevel@tonic-gate }
3347c478bd9Sstevel@tonic-gate }
3357c478bd9Sstevel@tonic-gate }
3367c478bd9Sstevel@tonic-gate return (status);
3377c478bd9Sstevel@tonic-gate }
3387c478bd9Sstevel@tonic-gate
3397c478bd9Sstevel@tonic-gate /*
3407c478bd9Sstevel@tonic-gate * chgrpr() - recursive chown()
3417c478bd9Sstevel@tonic-gate *
3427c478bd9Sstevel@tonic-gate * Recursively chowns the input directory then its contents. rflag must
3437c478bd9Sstevel@tonic-gate * have been set if chgrpr() is called. The input directory should not
3447c478bd9Sstevel@tonic-gate * be a sym link (this is handled in the calling routine). In
3457c478bd9Sstevel@tonic-gate * addition, the calling routine should have already added the input
3467c478bd9Sstevel@tonic-gate * directory to the search tree so we do not get into endless loops.
3477c478bd9Sstevel@tonic-gate * Note: chgrpr() doesn't need a return value as errors are reported
3487c478bd9Sstevel@tonic-gate * through the global "status" variable.
3497c478bd9Sstevel@tonic-gate */
3507c478bd9Sstevel@tonic-gate static void
chgrpr(char * dir,gid_t gid)3517c478bd9Sstevel@tonic-gate chgrpr(char *dir, gid_t gid)
3527c478bd9Sstevel@tonic-gate {
3537c478bd9Sstevel@tonic-gate struct dirent *dp;
3547c478bd9Sstevel@tonic-gate DIR *dirp;
3557c478bd9Sstevel@tonic-gate struct stat st, st2;
3567c478bd9Sstevel@tonic-gate char savedir[1024];
3577c478bd9Sstevel@tonic-gate
3587c478bd9Sstevel@tonic-gate if (getcwd(savedir, 1024) == 0) {
3597c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "chgrp: ");
3607c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s\n"), savedir);
3617c478bd9Sstevel@tonic-gate exit(255);
3627c478bd9Sstevel@tonic-gate }
3637c478bd9Sstevel@tonic-gate
3647c478bd9Sstevel@tonic-gate /*
3657c478bd9Sstevel@tonic-gate * Attempt to chown the directory, however don't return if we
3667c478bd9Sstevel@tonic-gate * can't as we still may be able to chown the contents of the
3677c478bd9Sstevel@tonic-gate * directory. Note: the calling routine resets the SUID bits
3687c478bd9Sstevel@tonic-gate * on this directory so we don't have to perform an extra 'stat'.
3697c478bd9Sstevel@tonic-gate */
3707c478bd9Sstevel@tonic-gate CHOWN(dir, -1, gid);
3717c478bd9Sstevel@tonic-gate
3727c478bd9Sstevel@tonic-gate if (chdir(dir) < 0) {
3737c478bd9Sstevel@tonic-gate status += Perror(dir);
3747c478bd9Sstevel@tonic-gate return;
3757c478bd9Sstevel@tonic-gate }
3767c478bd9Sstevel@tonic-gate if ((dirp = opendir(".")) == NULL) {
3777c478bd9Sstevel@tonic-gate status += Perror(dir);
3787c478bd9Sstevel@tonic-gate return;
3797c478bd9Sstevel@tonic-gate }
3807c478bd9Sstevel@tonic-gate for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) {
3817c478bd9Sstevel@tonic-gate if ((strcmp(dp->d_name, ".") == 0) ||
3827c478bd9Sstevel@tonic-gate (strcmp(dp->d_name, "..") == 0)) {
3837c478bd9Sstevel@tonic-gate continue; /* skip "." and ".." */
3847c478bd9Sstevel@tonic-gate }
3857c478bd9Sstevel@tonic-gate if (lstat(dp->d_name, &st) < 0) {
3867c478bd9Sstevel@tonic-gate status += Perror(dp->d_name);
3877c478bd9Sstevel@tonic-gate continue;
3887c478bd9Sstevel@tonic-gate }
3897c478bd9Sstevel@tonic-gate if ((st.st_mode & S_IFMT) == S_IFLNK) {
3907c478bd9Sstevel@tonic-gate if (hflag || Pflag) {
3917c478bd9Sstevel@tonic-gate /*
3927c478bd9Sstevel@tonic-gate * Change the group id of the symbolic link
3937c478bd9Sstevel@tonic-gate * encountered while traversing the
3947c478bd9Sstevel@tonic-gate * directory. Don't follow the symbolic
3957c478bd9Sstevel@tonic-gate * link to any other part of the file
3967c478bd9Sstevel@tonic-gate * hierarchy.
3977c478bd9Sstevel@tonic-gate */
3987c478bd9Sstevel@tonic-gate LCHOWN(dp->d_name, -1, gid);
3997c478bd9Sstevel@tonic-gate } else {
4007c478bd9Sstevel@tonic-gate if (stat(dp->d_name, &st2) < 0) {
4017c478bd9Sstevel@tonic-gate status += Perror(dp->d_name);
4027c478bd9Sstevel@tonic-gate continue;
4037c478bd9Sstevel@tonic-gate }
4047c478bd9Sstevel@tonic-gate /*
4057c478bd9Sstevel@tonic-gate * We know that we are to change the
4067c478bd9Sstevel@tonic-gate * group of the file referenced by the
4077c478bd9Sstevel@tonic-gate * symlink encountered while traversing
4087c478bd9Sstevel@tonic-gate * the directory. Now check to see if we
4097c478bd9Sstevel@tonic-gate * are to follow the symlink to any other
4107c478bd9Sstevel@tonic-gate * part of the file hierarchy.
4117c478bd9Sstevel@tonic-gate */
4127c478bd9Sstevel@tonic-gate if (FOLLOW_D_LINKS) {
4137c478bd9Sstevel@tonic-gate if ((st2.st_mode & S_IFMT) == S_IFDIR) {
4147c478bd9Sstevel@tonic-gate /*
4157c478bd9Sstevel@tonic-gate * We are following symlinks so
4167c478bd9Sstevel@tonic-gate * traverse into the directory.
4177c478bd9Sstevel@tonic-gate * Add this node to the search
4187c478bd9Sstevel@tonic-gate * tree so we don't get into an
4197c478bd9Sstevel@tonic-gate * endless loop.
4207c478bd9Sstevel@tonic-gate */
4217c478bd9Sstevel@tonic-gate int rc;
4227c478bd9Sstevel@tonic-gate if ((rc = add_tnode(&tree,
4237c478bd9Sstevel@tonic-gate st2.st_dev,
4247c478bd9Sstevel@tonic-gate st2.st_ino)) == 1) {
4257c478bd9Sstevel@tonic-gate chgrpr(dp->d_name, gid);
4267c478bd9Sstevel@tonic-gate
4277c478bd9Sstevel@tonic-gate /*
4287c478bd9Sstevel@tonic-gate * Restore SET[UG]ID
4297c478bd9Sstevel@tonic-gate * bits.
4307c478bd9Sstevel@tonic-gate */
4317c478bd9Sstevel@tonic-gate SETUGID_PRESERVE(
4327c478bd9Sstevel@tonic-gate dp->d_name,
4337c478bd9Sstevel@tonic-gate st2.st_mode &
4347c478bd9Sstevel@tonic-gate ~S_IFMT);
4357c478bd9Sstevel@tonic-gate } else if (rc == 0) {
4367c478bd9Sstevel@tonic-gate /* already visited */
4377c478bd9Sstevel@tonic-gate continue;
4387c478bd9Sstevel@tonic-gate } else {
4397c478bd9Sstevel@tonic-gate /*
4407c478bd9Sstevel@tonic-gate * An error occurred
4417c478bd9Sstevel@tonic-gate * while trying to add
4427c478bd9Sstevel@tonic-gate * the node to the tree.
4437c478bd9Sstevel@tonic-gate */
4447c478bd9Sstevel@tonic-gate status += Perror(
4457c478bd9Sstevel@tonic-gate dp->d_name);
4467c478bd9Sstevel@tonic-gate continue;
4477c478bd9Sstevel@tonic-gate }
4487c478bd9Sstevel@tonic-gate } else {
4497c478bd9Sstevel@tonic-gate /*
4507c478bd9Sstevel@tonic-gate * Change the group id of the
4517c478bd9Sstevel@tonic-gate * file referenced by the
4527c478bd9Sstevel@tonic-gate * symbolic link.
4537c478bd9Sstevel@tonic-gate */
4547c478bd9Sstevel@tonic-gate CHOWN(dp->d_name, -1, gid);
4557c478bd9Sstevel@tonic-gate
4567c478bd9Sstevel@tonic-gate }
4577c478bd9Sstevel@tonic-gate } else {
4587c478bd9Sstevel@tonic-gate /*
4597c478bd9Sstevel@tonic-gate * Change the group id of the file
4607c478bd9Sstevel@tonic-gate * referenced by the symbolic link.
4617c478bd9Sstevel@tonic-gate */
4627c478bd9Sstevel@tonic-gate CHOWN(dp->d_name, -1, gid);
4637c478bd9Sstevel@tonic-gate
4647c478bd9Sstevel@tonic-gate if ((st2.st_mode & S_IFMT) == S_IFDIR) {
4657c478bd9Sstevel@tonic-gate /* Restore SET[UG]ID bits. */
4667c478bd9Sstevel@tonic-gate SETUGID_PRESERVE(dp->d_name,
4677c478bd9Sstevel@tonic-gate st2.st_mode & ~S_IFMT);
4687c478bd9Sstevel@tonic-gate }
4697c478bd9Sstevel@tonic-gate }
4707c478bd9Sstevel@tonic-gate }
4717c478bd9Sstevel@tonic-gate } else if ((st.st_mode & S_IFMT) == S_IFDIR) {
4727c478bd9Sstevel@tonic-gate /*
4737c478bd9Sstevel@tonic-gate * Add this node to the search tree so we don't
4747c478bd9Sstevel@tonic-gate * get into a endless loop.
4757c478bd9Sstevel@tonic-gate */
4767c478bd9Sstevel@tonic-gate int rc;
4777c478bd9Sstevel@tonic-gate if ((rc = add_tnode(&tree, st.st_dev,
4787c478bd9Sstevel@tonic-gate st.st_ino)) == 1) {
4797c478bd9Sstevel@tonic-gate chgrpr(dp->d_name, gid);
4807c478bd9Sstevel@tonic-gate
4817c478bd9Sstevel@tonic-gate /* Restore the SET[UG]ID bits. */
4827c478bd9Sstevel@tonic-gate SETUGID_PRESERVE(dp->d_name,
4837c478bd9Sstevel@tonic-gate st.st_mode & ~S_IFMT);
4847c478bd9Sstevel@tonic-gate } else if (rc == 0) {
4857c478bd9Sstevel@tonic-gate /* already visited */
4867c478bd9Sstevel@tonic-gate continue;
4877c478bd9Sstevel@tonic-gate } else {
4887c478bd9Sstevel@tonic-gate /*
4897c478bd9Sstevel@tonic-gate * An error occurred while trying
4907c478bd9Sstevel@tonic-gate * to add the node to the search tree.
4917c478bd9Sstevel@tonic-gate */
4927c478bd9Sstevel@tonic-gate status += Perror(dp->d_name);
4937c478bd9Sstevel@tonic-gate continue;
4947c478bd9Sstevel@tonic-gate }
4957c478bd9Sstevel@tonic-gate } else {
4967c478bd9Sstevel@tonic-gate CHOWN(dp->d_name, -1, gid);
4977c478bd9Sstevel@tonic-gate }
4987c478bd9Sstevel@tonic-gate }
4997c478bd9Sstevel@tonic-gate (void) closedir(dirp);
5007c478bd9Sstevel@tonic-gate if (chdir(savedir) < 0) {
5017c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "chgrp: ");
5027c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("can't change back to %s\n"),
5037c478bd9Sstevel@tonic-gate savedir);
5047c478bd9Sstevel@tonic-gate exit(255);
5057c478bd9Sstevel@tonic-gate }
5067c478bd9Sstevel@tonic-gate }
5077c478bd9Sstevel@tonic-gate
5087c478bd9Sstevel@tonic-gate static int
isnumber(char * s)5097c478bd9Sstevel@tonic-gate isnumber(char *s)
5107c478bd9Sstevel@tonic-gate {
5117c478bd9Sstevel@tonic-gate int c;
5127c478bd9Sstevel@tonic-gate
5137c478bd9Sstevel@tonic-gate while ((c = *s++) != '\0')
5147c478bd9Sstevel@tonic-gate if (!isdigit(c))
5157c478bd9Sstevel@tonic-gate return (0);
5167c478bd9Sstevel@tonic-gate return (1);
5177c478bd9Sstevel@tonic-gate }
5187c478bd9Sstevel@tonic-gate
5197c478bd9Sstevel@tonic-gate
5207c478bd9Sstevel@tonic-gate static int
Perror(char * s)5217c478bd9Sstevel@tonic-gate Perror(char *s)
5227c478bd9Sstevel@tonic-gate {
5237c478bd9Sstevel@tonic-gate if (!fflag) {
5247c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "chgrp: ");
5257c478bd9Sstevel@tonic-gate perror(s);
5267c478bd9Sstevel@tonic-gate }
5277c478bd9Sstevel@tonic-gate return (!fflag);
5287c478bd9Sstevel@tonic-gate }
5297c478bd9Sstevel@tonic-gate
5307c478bd9Sstevel@tonic-gate
5317c478bd9Sstevel@tonic-gate static void
usage(void)5327c478bd9Sstevel@tonic-gate usage(void)
5337c478bd9Sstevel@tonic-gate {
5347c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(
5357c478bd9Sstevel@tonic-gate "usage:\n"
5367c478bd9Sstevel@tonic-gate "\tchgrp [-fhR] group file ...\n"
537b249c65cSmarks "\tchgrp -R [-f] [-H|-L|-P] group file ...\n"
538b249c65cSmarks "\tchgrp -s [-fhR] groupsid file ...\n"
539b249c65cSmarks "\tchgrp -s -R [-f] [-H|-L|-P] groupsid file ...\n"));
5407c478bd9Sstevel@tonic-gate exit(2);
5417c478bd9Sstevel@tonic-gate }
542