1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 25*7c478bd9Sstevel@tonic-gate */ 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate /* 28*7c478bd9Sstevel@tonic-gate * Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T 29*7c478bd9Sstevel@tonic-gate * All Rights Reserved 30*7c478bd9Sstevel@tonic-gate */ 31*7c478bd9Sstevel@tonic-gate 32*7c478bd9Sstevel@tonic-gate /* 33*7c478bd9Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988 34*7c478bd9Sstevel@tonic-gate * The Regents of the University of California 35*7c478bd9Sstevel@tonic-gate * All Rights Reserved 36*7c478bd9Sstevel@tonic-gate * 37*7c478bd9Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from 38*7c478bd9Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its 39*7c478bd9Sstevel@tonic-gate * contributors. 40*7c478bd9Sstevel@tonic-gate */ 41*7c478bd9Sstevel@tonic-gate 42*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 43*7c478bd9Sstevel@tonic-gate 44*7c478bd9Sstevel@tonic-gate /* 45*7c478bd9Sstevel@tonic-gate * chgrp [-fhR] gid file ... 46*7c478bd9Sstevel@tonic-gate * chgrp -R [-f] [-H|-L|-P] gid file ... 47*7c478bd9Sstevel@tonic-gate */ 48*7c478bd9Sstevel@tonic-gate 49*7c478bd9Sstevel@tonic-gate #include <stdio.h> 50*7c478bd9Sstevel@tonic-gate #include <ctype.h> 51*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 52*7c478bd9Sstevel@tonic-gate #include <sys/stat.h> 53*7c478bd9Sstevel@tonic-gate #include <sys/avl.h> 54*7c478bd9Sstevel@tonic-gate #include <grp.h> 55*7c478bd9Sstevel@tonic-gate #include <dirent.h> 56*7c478bd9Sstevel@tonic-gate #include <unistd.h> 57*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 58*7c478bd9Sstevel@tonic-gate #include <locale.h> 59*7c478bd9Sstevel@tonic-gate #include <libcmdutils.h> 60*7c478bd9Sstevel@tonic-gate #include <errno.h> 61*7c478bd9Sstevel@tonic-gate #include <strings.h> 62*7c478bd9Sstevel@tonic-gate 63*7c478bd9Sstevel@tonic-gate static struct group *gr; 64*7c478bd9Sstevel@tonic-gate static struct stat stbuf; 65*7c478bd9Sstevel@tonic-gate static struct stat stbuf2; 66*7c478bd9Sstevel@tonic-gate static gid_t gid; 67*7c478bd9Sstevel@tonic-gate static int hflag = 0, 68*7c478bd9Sstevel@tonic-gate fflag = 0, 69*7c478bd9Sstevel@tonic-gate rflag = 0, 70*7c478bd9Sstevel@tonic-gate Hflag = 0, 71*7c478bd9Sstevel@tonic-gate Lflag = 0, 72*7c478bd9Sstevel@tonic-gate Pflag = 0; 73*7c478bd9Sstevel@tonic-gate static int status = 0; /* total number of errors received */ 74*7c478bd9Sstevel@tonic-gate 75*7c478bd9Sstevel@tonic-gate static avl_tree_t *tree; /* search tree to store inode data */ 76*7c478bd9Sstevel@tonic-gate 77*7c478bd9Sstevel@tonic-gate static void usage(void); 78*7c478bd9Sstevel@tonic-gate static int isnumber(char *); 79*7c478bd9Sstevel@tonic-gate static int Perror(char *); 80*7c478bd9Sstevel@tonic-gate static void chgrpr(char *, gid_t); 81*7c478bd9Sstevel@tonic-gate 82*7c478bd9Sstevel@tonic-gate #ifdef XPG4 83*7c478bd9Sstevel@tonic-gate /* 84*7c478bd9Sstevel@tonic-gate * Check to see if we are to follow symlinks specified on the command line. 85*7c478bd9Sstevel@tonic-gate * This assumes we've already checked to make sure neither -h or -P was 86*7c478bd9Sstevel@tonic-gate * specified, so we are just looking to see if -R -L, or -R -H was specified, 87*7c478bd9Sstevel@tonic-gate * or, since -R has the same behavior as -R -L, if -R was specified by itself. 88*7c478bd9Sstevel@tonic-gate * Therefore, all we really need to check for is if -R was specified. 89*7c478bd9Sstevel@tonic-gate */ 90*7c478bd9Sstevel@tonic-gate #define FOLLOW_CL_LINKS (rflag) 91*7c478bd9Sstevel@tonic-gate #else 92*7c478bd9Sstevel@tonic-gate /* 93*7c478bd9Sstevel@tonic-gate * Check to see if we are to follow symlinks specified on the command line. 94*7c478bd9Sstevel@tonic-gate * This assumes we've already checked to make sure neither -h or -P was 95*7c478bd9Sstevel@tonic-gate * specified, so we are just looking to see if -R -L, or -R -H was specified. 96*7c478bd9Sstevel@tonic-gate * Note: -R by itself will change the group of a directory referenced by a 97*7c478bd9Sstevel@tonic-gate * symlink however it will not follow the symlink to any other part of the 98*7c478bd9Sstevel@tonic-gate * file hierarchy. 99*7c478bd9Sstevel@tonic-gate */ 100*7c478bd9Sstevel@tonic-gate #define FOLLOW_CL_LINKS (rflag && (Hflag || Lflag)) 101*7c478bd9Sstevel@tonic-gate #endif 102*7c478bd9Sstevel@tonic-gate 103*7c478bd9Sstevel@tonic-gate #ifdef XPG4 104*7c478bd9Sstevel@tonic-gate /* 105*7c478bd9Sstevel@tonic-gate * Follow symlinks when traversing directories. Since -R behaves the 106*7c478bd9Sstevel@tonic-gate * same as -R -L, we always want to follow symlinks to other parts 107*7c478bd9Sstevel@tonic-gate * of the file hierarchy unless -H was specified. 108*7c478bd9Sstevel@tonic-gate */ 109*7c478bd9Sstevel@tonic-gate #define FOLLOW_D_LINKS (!Hflag) 110*7c478bd9Sstevel@tonic-gate #else 111*7c478bd9Sstevel@tonic-gate /* 112*7c478bd9Sstevel@tonic-gate * Follow symlinks when traversing directories. Only follow symlinks 113*7c478bd9Sstevel@tonic-gate * to other parts of the file hierarchy if -L was specified. 114*7c478bd9Sstevel@tonic-gate */ 115*7c478bd9Sstevel@tonic-gate #define FOLLOW_D_LINKS (Lflag) 116*7c478bd9Sstevel@tonic-gate #endif 117*7c478bd9Sstevel@tonic-gate 118*7c478bd9Sstevel@tonic-gate #define CHOWN(f, u, g) if (chown(f, u, g) < 0) { \ 119*7c478bd9Sstevel@tonic-gate status += Perror(f); \ 120*7c478bd9Sstevel@tonic-gate } 121*7c478bd9Sstevel@tonic-gate 122*7c478bd9Sstevel@tonic-gate #define LCHOWN(f, u, g) if (lchown(f, u, g) < 0) { \ 123*7c478bd9Sstevel@tonic-gate status += Perror(f); \ 124*7c478bd9Sstevel@tonic-gate } 125*7c478bd9Sstevel@tonic-gate /* 126*7c478bd9Sstevel@tonic-gate * We're ignoring errors here because preserving the SET[UG]ID bits is just 127*7c478bd9Sstevel@tonic-gate * a courtesy. This is only used on directories. 128*7c478bd9Sstevel@tonic-gate */ 129*7c478bd9Sstevel@tonic-gate #define SETUGID_PRESERVE(dir, mode) \ 130*7c478bd9Sstevel@tonic-gate if (((mode) & (S_ISGID|S_ISUID)) != 0) \ 131*7c478bd9Sstevel@tonic-gate (void) chmod((dir), (mode) & ~S_IFMT) 132*7c478bd9Sstevel@tonic-gate 133*7c478bd9Sstevel@tonic-gate extern int optind; 134*7c478bd9Sstevel@tonic-gate 135*7c478bd9Sstevel@tonic-gate 136*7c478bd9Sstevel@tonic-gate int 137*7c478bd9Sstevel@tonic-gate main(int argc, char *argv[]) 138*7c478bd9Sstevel@tonic-gate { 139*7c478bd9Sstevel@tonic-gate int c; 140*7c478bd9Sstevel@tonic-gate 141*7c478bd9Sstevel@tonic-gate /* set the locale for only the messages system (all else is clean) */ 142*7c478bd9Sstevel@tonic-gate 143*7c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 144*7c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */ 145*7c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */ 146*7c478bd9Sstevel@tonic-gate #endif 147*7c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 148*7c478bd9Sstevel@tonic-gate 149*7c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "RhfHLP")) != EOF) 150*7c478bd9Sstevel@tonic-gate switch (c) { 151*7c478bd9Sstevel@tonic-gate case 'R': 152*7c478bd9Sstevel@tonic-gate rflag++; 153*7c478bd9Sstevel@tonic-gate break; 154*7c478bd9Sstevel@tonic-gate case 'h': 155*7c478bd9Sstevel@tonic-gate hflag++; 156*7c478bd9Sstevel@tonic-gate break; 157*7c478bd9Sstevel@tonic-gate case 'f': 158*7c478bd9Sstevel@tonic-gate fflag++; 159*7c478bd9Sstevel@tonic-gate break; 160*7c478bd9Sstevel@tonic-gate case 'H': 161*7c478bd9Sstevel@tonic-gate /* 162*7c478bd9Sstevel@tonic-gate * If more than one of -H, -L, and -P 163*7c478bd9Sstevel@tonic-gate * are specified, only the last option 164*7c478bd9Sstevel@tonic-gate * specified determines the behavior of 165*7c478bd9Sstevel@tonic-gate * chgrp. In addition, make [-H|-L] 166*7c478bd9Sstevel@tonic-gate * mutually exclusive of -h. 167*7c478bd9Sstevel@tonic-gate */ 168*7c478bd9Sstevel@tonic-gate Lflag = Pflag = 0; 169*7c478bd9Sstevel@tonic-gate Hflag++; 170*7c478bd9Sstevel@tonic-gate break; 171*7c478bd9Sstevel@tonic-gate case 'L': 172*7c478bd9Sstevel@tonic-gate Hflag = Pflag = 0; 173*7c478bd9Sstevel@tonic-gate Lflag++; 174*7c478bd9Sstevel@tonic-gate break; 175*7c478bd9Sstevel@tonic-gate case 'P': 176*7c478bd9Sstevel@tonic-gate Hflag = Lflag = 0; 177*7c478bd9Sstevel@tonic-gate Pflag++; 178*7c478bd9Sstevel@tonic-gate break; 179*7c478bd9Sstevel@tonic-gate default: 180*7c478bd9Sstevel@tonic-gate usage(); 181*7c478bd9Sstevel@tonic-gate } 182*7c478bd9Sstevel@tonic-gate 183*7c478bd9Sstevel@tonic-gate /* 184*7c478bd9Sstevel@tonic-gate * Check for sufficient arguments 185*7c478bd9Sstevel@tonic-gate * or a usage error. 186*7c478bd9Sstevel@tonic-gate */ 187*7c478bd9Sstevel@tonic-gate argc -= optind; 188*7c478bd9Sstevel@tonic-gate argv = &argv[optind]; 189*7c478bd9Sstevel@tonic-gate 190*7c478bd9Sstevel@tonic-gate if ((argc < 2) || 191*7c478bd9Sstevel@tonic-gate ((Hflag || Lflag || Pflag) && !rflag) || 192*7c478bd9Sstevel@tonic-gate ((Hflag || Lflag || Pflag) && hflag)) { 193*7c478bd9Sstevel@tonic-gate usage(); 194*7c478bd9Sstevel@tonic-gate } 195*7c478bd9Sstevel@tonic-gate 196*7c478bd9Sstevel@tonic-gate if (isnumber(argv[0])) { 197*7c478bd9Sstevel@tonic-gate errno = 0; 198*7c478bd9Sstevel@tonic-gate gid = (gid_t)strtol(argv[0], NULL, 10); /* gid is an int */ 199*7c478bd9Sstevel@tonic-gate if (errno != 0) { 200*7c478bd9Sstevel@tonic-gate if (errno == ERANGE) { 201*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 202*7c478bd9Sstevel@tonic-gate "chgrp: group id is too large\n")); 203*7c478bd9Sstevel@tonic-gate exit(2); 204*7c478bd9Sstevel@tonic-gate } else { 205*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 206*7c478bd9Sstevel@tonic-gate "chgrp: invalid group id\n")); 207*7c478bd9Sstevel@tonic-gate exit(2); 208*7c478bd9Sstevel@tonic-gate } 209*7c478bd9Sstevel@tonic-gate } 210*7c478bd9Sstevel@tonic-gate } else { 211*7c478bd9Sstevel@tonic-gate if ((gr = getgrnam(argv[0])) == NULL) { 212*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "chgrp: "); 213*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("unknown group: %s\n"), 214*7c478bd9Sstevel@tonic-gate argv[0]); 215*7c478bd9Sstevel@tonic-gate exit(2); 216*7c478bd9Sstevel@tonic-gate } 217*7c478bd9Sstevel@tonic-gate gid = gr->gr_gid; 218*7c478bd9Sstevel@tonic-gate } 219*7c478bd9Sstevel@tonic-gate 220*7c478bd9Sstevel@tonic-gate for (c = 1; c < argc; c++) { 221*7c478bd9Sstevel@tonic-gate tree = NULL; 222*7c478bd9Sstevel@tonic-gate if (lstat(argv[c], &stbuf) < 0) { 223*7c478bd9Sstevel@tonic-gate status += Perror(argv[c]); 224*7c478bd9Sstevel@tonic-gate continue; 225*7c478bd9Sstevel@tonic-gate } 226*7c478bd9Sstevel@tonic-gate if (rflag && ((stbuf.st_mode & S_IFMT) == S_IFLNK)) { 227*7c478bd9Sstevel@tonic-gate if (hflag || Pflag) { 228*7c478bd9Sstevel@tonic-gate /* 229*7c478bd9Sstevel@tonic-gate * Change the group id of the symbolic link 230*7c478bd9Sstevel@tonic-gate * specified on the command line. 231*7c478bd9Sstevel@tonic-gate * Don't follow the symbolic link to 232*7c478bd9Sstevel@tonic-gate * any other part of the file hierarchy. 233*7c478bd9Sstevel@tonic-gate */ 234*7c478bd9Sstevel@tonic-gate LCHOWN(argv[c], -1, gid); 235*7c478bd9Sstevel@tonic-gate } else { 236*7c478bd9Sstevel@tonic-gate if (stat(argv[c], &stbuf2) < 0) { 237*7c478bd9Sstevel@tonic-gate status += Perror(argv[c]); 238*7c478bd9Sstevel@tonic-gate continue; 239*7c478bd9Sstevel@tonic-gate } 240*7c478bd9Sstevel@tonic-gate /* 241*7c478bd9Sstevel@tonic-gate * We know that we are to change the 242*7c478bd9Sstevel@tonic-gate * group of the file referenced by the 243*7c478bd9Sstevel@tonic-gate * symlink specified on the command line. 244*7c478bd9Sstevel@tonic-gate * Now check to see if we are to follow 245*7c478bd9Sstevel@tonic-gate * the symlink to any other part of the 246*7c478bd9Sstevel@tonic-gate * file hierarchy. 247*7c478bd9Sstevel@tonic-gate */ 248*7c478bd9Sstevel@tonic-gate if (FOLLOW_CL_LINKS) { 249*7c478bd9Sstevel@tonic-gate if ((stbuf2.st_mode & S_IFMT) 250*7c478bd9Sstevel@tonic-gate == S_IFDIR) { 251*7c478bd9Sstevel@tonic-gate /* 252*7c478bd9Sstevel@tonic-gate * We are following symlinks so 253*7c478bd9Sstevel@tonic-gate * traverse into the directory. 254*7c478bd9Sstevel@tonic-gate * Add this node to the search 255*7c478bd9Sstevel@tonic-gate * tree so we don't get into an 256*7c478bd9Sstevel@tonic-gate * endless loop. 257*7c478bd9Sstevel@tonic-gate */ 258*7c478bd9Sstevel@tonic-gate if (add_tnode(&tree, 259*7c478bd9Sstevel@tonic-gate stbuf2.st_dev, 260*7c478bd9Sstevel@tonic-gate stbuf2.st_ino) == 1) { 261*7c478bd9Sstevel@tonic-gate chgrpr(argv[c], gid); 262*7c478bd9Sstevel@tonic-gate /* 263*7c478bd9Sstevel@tonic-gate * Try to restore the 264*7c478bd9Sstevel@tonic-gate * SET[UG]ID bits. 265*7c478bd9Sstevel@tonic-gate */ 266*7c478bd9Sstevel@tonic-gate SETUGID_PRESERVE( 267*7c478bd9Sstevel@tonic-gate argv[c], 268*7c478bd9Sstevel@tonic-gate stbuf2.st_mode & 269*7c478bd9Sstevel@tonic-gate ~S_IFMT); 270*7c478bd9Sstevel@tonic-gate } else { 271*7c478bd9Sstevel@tonic-gate /* 272*7c478bd9Sstevel@tonic-gate * Error occurred. 273*7c478bd9Sstevel@tonic-gate * rc can't be 0 274*7c478bd9Sstevel@tonic-gate * as this is the first 275*7c478bd9Sstevel@tonic-gate * node to be added to 276*7c478bd9Sstevel@tonic-gate * the search tree. 277*7c478bd9Sstevel@tonic-gate */ 278*7c478bd9Sstevel@tonic-gate status += Perror( 279*7c478bd9Sstevel@tonic-gate argv[c]); 280*7c478bd9Sstevel@tonic-gate } 281*7c478bd9Sstevel@tonic-gate } else { 282*7c478bd9Sstevel@tonic-gate /* 283*7c478bd9Sstevel@tonic-gate * Change the group id of the 284*7c478bd9Sstevel@tonic-gate * file referenced by the 285*7c478bd9Sstevel@tonic-gate * symbolic link. 286*7c478bd9Sstevel@tonic-gate */ 287*7c478bd9Sstevel@tonic-gate CHOWN(argv[c], -1, gid); 288*7c478bd9Sstevel@tonic-gate } 289*7c478bd9Sstevel@tonic-gate } else { 290*7c478bd9Sstevel@tonic-gate /* 291*7c478bd9Sstevel@tonic-gate * Change the group id of the file 292*7c478bd9Sstevel@tonic-gate * referenced by the symbolic link. 293*7c478bd9Sstevel@tonic-gate */ 294*7c478bd9Sstevel@tonic-gate CHOWN(argv[c], -1, gid); 295*7c478bd9Sstevel@tonic-gate 296*7c478bd9Sstevel@tonic-gate if ((stbuf2.st_mode & S_IFMT) 297*7c478bd9Sstevel@tonic-gate == S_IFDIR) { 298*7c478bd9Sstevel@tonic-gate /* Reset the SET[UG]ID bits. */ 299*7c478bd9Sstevel@tonic-gate SETUGID_PRESERVE(argv[c], 300*7c478bd9Sstevel@tonic-gate stbuf2.st_mode & ~S_IFMT); 301*7c478bd9Sstevel@tonic-gate } 302*7c478bd9Sstevel@tonic-gate } 303*7c478bd9Sstevel@tonic-gate } 304*7c478bd9Sstevel@tonic-gate } else if (rflag && ((stbuf.st_mode & S_IFMT) == S_IFDIR)) { 305*7c478bd9Sstevel@tonic-gate /* 306*7c478bd9Sstevel@tonic-gate * Add this node to the search tree so we don't 307*7c478bd9Sstevel@tonic-gate * get into a endless loop. 308*7c478bd9Sstevel@tonic-gate */ 309*7c478bd9Sstevel@tonic-gate if (add_tnode(&tree, stbuf.st_dev, 310*7c478bd9Sstevel@tonic-gate stbuf.st_ino) == 1) { 311*7c478bd9Sstevel@tonic-gate chgrpr(argv[c], gid); 312*7c478bd9Sstevel@tonic-gate 313*7c478bd9Sstevel@tonic-gate /* Restore the SET[UG]ID bits. */ 314*7c478bd9Sstevel@tonic-gate SETUGID_PRESERVE(argv[c], 315*7c478bd9Sstevel@tonic-gate stbuf.st_mode & ~S_IFMT); 316*7c478bd9Sstevel@tonic-gate } else { 317*7c478bd9Sstevel@tonic-gate /* 318*7c478bd9Sstevel@tonic-gate * An error occurred while trying 319*7c478bd9Sstevel@tonic-gate * to add the node to the tree. 320*7c478bd9Sstevel@tonic-gate * Continue on with next file 321*7c478bd9Sstevel@tonic-gate * specified. Note: rc shouldn't 322*7c478bd9Sstevel@tonic-gate * be 0 as this was the first node 323*7c478bd9Sstevel@tonic-gate * being added to the search tree. 324*7c478bd9Sstevel@tonic-gate */ 325*7c478bd9Sstevel@tonic-gate status += Perror(argv[c]); 326*7c478bd9Sstevel@tonic-gate } 327*7c478bd9Sstevel@tonic-gate } else { 328*7c478bd9Sstevel@tonic-gate if (hflag || Pflag) { 329*7c478bd9Sstevel@tonic-gate LCHOWN(argv[c], -1, gid); 330*7c478bd9Sstevel@tonic-gate } else { 331*7c478bd9Sstevel@tonic-gate CHOWN(argv[c], -1, gid); 332*7c478bd9Sstevel@tonic-gate } 333*7c478bd9Sstevel@tonic-gate /* If a directory, reset the SET[UG]ID bits. */ 334*7c478bd9Sstevel@tonic-gate if ((stbuf.st_mode & S_IFMT) == S_IFDIR) { 335*7c478bd9Sstevel@tonic-gate SETUGID_PRESERVE(argv[c], 336*7c478bd9Sstevel@tonic-gate stbuf.st_mode & ~S_IFMT); 337*7c478bd9Sstevel@tonic-gate } 338*7c478bd9Sstevel@tonic-gate } 339*7c478bd9Sstevel@tonic-gate } 340*7c478bd9Sstevel@tonic-gate return (status); 341*7c478bd9Sstevel@tonic-gate } 342*7c478bd9Sstevel@tonic-gate 343*7c478bd9Sstevel@tonic-gate /* 344*7c478bd9Sstevel@tonic-gate * chgrpr() - recursive chown() 345*7c478bd9Sstevel@tonic-gate * 346*7c478bd9Sstevel@tonic-gate * Recursively chowns the input directory then its contents. rflag must 347*7c478bd9Sstevel@tonic-gate * have been set if chgrpr() is called. The input directory should not 348*7c478bd9Sstevel@tonic-gate * be a sym link (this is handled in the calling routine). In 349*7c478bd9Sstevel@tonic-gate * addition, the calling routine should have already added the input 350*7c478bd9Sstevel@tonic-gate * directory to the search tree so we do not get into endless loops. 351*7c478bd9Sstevel@tonic-gate * Note: chgrpr() doesn't need a return value as errors are reported 352*7c478bd9Sstevel@tonic-gate * through the global "status" variable. 353*7c478bd9Sstevel@tonic-gate */ 354*7c478bd9Sstevel@tonic-gate static void 355*7c478bd9Sstevel@tonic-gate chgrpr(char *dir, gid_t gid) 356*7c478bd9Sstevel@tonic-gate { 357*7c478bd9Sstevel@tonic-gate struct dirent *dp; 358*7c478bd9Sstevel@tonic-gate DIR *dirp; 359*7c478bd9Sstevel@tonic-gate struct stat st, st2; 360*7c478bd9Sstevel@tonic-gate char savedir[1024]; 361*7c478bd9Sstevel@tonic-gate 362*7c478bd9Sstevel@tonic-gate if (getcwd(savedir, 1024) == 0) { 363*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "chgrp: "); 364*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s\n"), savedir); 365*7c478bd9Sstevel@tonic-gate exit(255); 366*7c478bd9Sstevel@tonic-gate } 367*7c478bd9Sstevel@tonic-gate 368*7c478bd9Sstevel@tonic-gate /* 369*7c478bd9Sstevel@tonic-gate * Attempt to chown the directory, however don't return if we 370*7c478bd9Sstevel@tonic-gate * can't as we still may be able to chown the contents of the 371*7c478bd9Sstevel@tonic-gate * directory. Note: the calling routine resets the SUID bits 372*7c478bd9Sstevel@tonic-gate * on this directory so we don't have to perform an extra 'stat'. 373*7c478bd9Sstevel@tonic-gate */ 374*7c478bd9Sstevel@tonic-gate CHOWN(dir, -1, gid); 375*7c478bd9Sstevel@tonic-gate 376*7c478bd9Sstevel@tonic-gate if (chdir(dir) < 0) { 377*7c478bd9Sstevel@tonic-gate status += Perror(dir); 378*7c478bd9Sstevel@tonic-gate return; 379*7c478bd9Sstevel@tonic-gate } 380*7c478bd9Sstevel@tonic-gate if ((dirp = opendir(".")) == NULL) { 381*7c478bd9Sstevel@tonic-gate status += Perror(dir); 382*7c478bd9Sstevel@tonic-gate return; 383*7c478bd9Sstevel@tonic-gate } 384*7c478bd9Sstevel@tonic-gate for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) { 385*7c478bd9Sstevel@tonic-gate if ((strcmp(dp->d_name, ".") == 0) || 386*7c478bd9Sstevel@tonic-gate (strcmp(dp->d_name, "..") == 0)) { 387*7c478bd9Sstevel@tonic-gate continue; /* skip "." and ".." */ 388*7c478bd9Sstevel@tonic-gate } 389*7c478bd9Sstevel@tonic-gate if (lstat(dp->d_name, &st) < 0) { 390*7c478bd9Sstevel@tonic-gate status += Perror(dp->d_name); 391*7c478bd9Sstevel@tonic-gate continue; 392*7c478bd9Sstevel@tonic-gate } 393*7c478bd9Sstevel@tonic-gate if ((st.st_mode & S_IFMT) == S_IFLNK) { 394*7c478bd9Sstevel@tonic-gate if (hflag || Pflag) { 395*7c478bd9Sstevel@tonic-gate /* 396*7c478bd9Sstevel@tonic-gate * Change the group id of the symbolic link 397*7c478bd9Sstevel@tonic-gate * encountered while traversing the 398*7c478bd9Sstevel@tonic-gate * directory. Don't follow the symbolic 399*7c478bd9Sstevel@tonic-gate * link to any other part of the file 400*7c478bd9Sstevel@tonic-gate * hierarchy. 401*7c478bd9Sstevel@tonic-gate */ 402*7c478bd9Sstevel@tonic-gate LCHOWN(dp->d_name, -1, gid); 403*7c478bd9Sstevel@tonic-gate } else { 404*7c478bd9Sstevel@tonic-gate if (stat(dp->d_name, &st2) < 0) { 405*7c478bd9Sstevel@tonic-gate status += Perror(dp->d_name); 406*7c478bd9Sstevel@tonic-gate continue; 407*7c478bd9Sstevel@tonic-gate } 408*7c478bd9Sstevel@tonic-gate /* 409*7c478bd9Sstevel@tonic-gate * We know that we are to change the 410*7c478bd9Sstevel@tonic-gate * group of the file referenced by the 411*7c478bd9Sstevel@tonic-gate * symlink encountered while traversing 412*7c478bd9Sstevel@tonic-gate * the directory. Now check to see if we 413*7c478bd9Sstevel@tonic-gate * are to follow the symlink to any other 414*7c478bd9Sstevel@tonic-gate * part of the file hierarchy. 415*7c478bd9Sstevel@tonic-gate */ 416*7c478bd9Sstevel@tonic-gate if (FOLLOW_D_LINKS) { 417*7c478bd9Sstevel@tonic-gate if ((st2.st_mode & S_IFMT) == S_IFDIR) { 418*7c478bd9Sstevel@tonic-gate /* 419*7c478bd9Sstevel@tonic-gate * We are following symlinks so 420*7c478bd9Sstevel@tonic-gate * traverse into the directory. 421*7c478bd9Sstevel@tonic-gate * Add this node to the search 422*7c478bd9Sstevel@tonic-gate * tree so we don't get into an 423*7c478bd9Sstevel@tonic-gate * endless loop. 424*7c478bd9Sstevel@tonic-gate */ 425*7c478bd9Sstevel@tonic-gate int rc; 426*7c478bd9Sstevel@tonic-gate if ((rc = add_tnode(&tree, 427*7c478bd9Sstevel@tonic-gate st2.st_dev, 428*7c478bd9Sstevel@tonic-gate st2.st_ino)) == 1) { 429*7c478bd9Sstevel@tonic-gate chgrpr(dp->d_name, gid); 430*7c478bd9Sstevel@tonic-gate 431*7c478bd9Sstevel@tonic-gate /* 432*7c478bd9Sstevel@tonic-gate * Restore SET[UG]ID 433*7c478bd9Sstevel@tonic-gate * bits. 434*7c478bd9Sstevel@tonic-gate */ 435*7c478bd9Sstevel@tonic-gate SETUGID_PRESERVE( 436*7c478bd9Sstevel@tonic-gate dp->d_name, 437*7c478bd9Sstevel@tonic-gate st2.st_mode & 438*7c478bd9Sstevel@tonic-gate ~S_IFMT); 439*7c478bd9Sstevel@tonic-gate } else if (rc == 0) { 440*7c478bd9Sstevel@tonic-gate /* already visited */ 441*7c478bd9Sstevel@tonic-gate continue; 442*7c478bd9Sstevel@tonic-gate } else { 443*7c478bd9Sstevel@tonic-gate /* 444*7c478bd9Sstevel@tonic-gate * An error occurred 445*7c478bd9Sstevel@tonic-gate * while trying to add 446*7c478bd9Sstevel@tonic-gate * the node to the tree. 447*7c478bd9Sstevel@tonic-gate */ 448*7c478bd9Sstevel@tonic-gate status += Perror( 449*7c478bd9Sstevel@tonic-gate dp->d_name); 450*7c478bd9Sstevel@tonic-gate continue; 451*7c478bd9Sstevel@tonic-gate } 452*7c478bd9Sstevel@tonic-gate } else { 453*7c478bd9Sstevel@tonic-gate /* 454*7c478bd9Sstevel@tonic-gate * Change the group id of the 455*7c478bd9Sstevel@tonic-gate * file referenced by the 456*7c478bd9Sstevel@tonic-gate * symbolic link. 457*7c478bd9Sstevel@tonic-gate */ 458*7c478bd9Sstevel@tonic-gate CHOWN(dp->d_name, -1, gid); 459*7c478bd9Sstevel@tonic-gate 460*7c478bd9Sstevel@tonic-gate } 461*7c478bd9Sstevel@tonic-gate } else { 462*7c478bd9Sstevel@tonic-gate /* 463*7c478bd9Sstevel@tonic-gate * Change the group id of the file 464*7c478bd9Sstevel@tonic-gate * referenced by the symbolic link. 465*7c478bd9Sstevel@tonic-gate */ 466*7c478bd9Sstevel@tonic-gate CHOWN(dp->d_name, -1, gid); 467*7c478bd9Sstevel@tonic-gate 468*7c478bd9Sstevel@tonic-gate if ((st2.st_mode & S_IFMT) == S_IFDIR) { 469*7c478bd9Sstevel@tonic-gate /* Restore SET[UG]ID bits. */ 470*7c478bd9Sstevel@tonic-gate SETUGID_PRESERVE(dp->d_name, 471*7c478bd9Sstevel@tonic-gate st2.st_mode & ~S_IFMT); 472*7c478bd9Sstevel@tonic-gate } 473*7c478bd9Sstevel@tonic-gate } 474*7c478bd9Sstevel@tonic-gate } 475*7c478bd9Sstevel@tonic-gate } else if ((st.st_mode & S_IFMT) == S_IFDIR) { 476*7c478bd9Sstevel@tonic-gate /* 477*7c478bd9Sstevel@tonic-gate * Add this node to the search tree so we don't 478*7c478bd9Sstevel@tonic-gate * get into a endless loop. 479*7c478bd9Sstevel@tonic-gate */ 480*7c478bd9Sstevel@tonic-gate int rc; 481*7c478bd9Sstevel@tonic-gate if ((rc = add_tnode(&tree, st.st_dev, 482*7c478bd9Sstevel@tonic-gate st.st_ino)) == 1) { 483*7c478bd9Sstevel@tonic-gate chgrpr(dp->d_name, gid); 484*7c478bd9Sstevel@tonic-gate 485*7c478bd9Sstevel@tonic-gate /* Restore the SET[UG]ID bits. */ 486*7c478bd9Sstevel@tonic-gate SETUGID_PRESERVE(dp->d_name, 487*7c478bd9Sstevel@tonic-gate st.st_mode & ~S_IFMT); 488*7c478bd9Sstevel@tonic-gate } else if (rc == 0) { 489*7c478bd9Sstevel@tonic-gate /* already visited */ 490*7c478bd9Sstevel@tonic-gate continue; 491*7c478bd9Sstevel@tonic-gate } else { 492*7c478bd9Sstevel@tonic-gate /* 493*7c478bd9Sstevel@tonic-gate * An error occurred while trying 494*7c478bd9Sstevel@tonic-gate * to add the node to the search tree. 495*7c478bd9Sstevel@tonic-gate */ 496*7c478bd9Sstevel@tonic-gate status += Perror(dp->d_name); 497*7c478bd9Sstevel@tonic-gate continue; 498*7c478bd9Sstevel@tonic-gate } 499*7c478bd9Sstevel@tonic-gate } else { 500*7c478bd9Sstevel@tonic-gate CHOWN(dp->d_name, -1, gid); 501*7c478bd9Sstevel@tonic-gate } 502*7c478bd9Sstevel@tonic-gate } 503*7c478bd9Sstevel@tonic-gate (void) closedir(dirp); 504*7c478bd9Sstevel@tonic-gate if (chdir(savedir) < 0) { 505*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "chgrp: "); 506*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("can't change back to %s\n"), 507*7c478bd9Sstevel@tonic-gate savedir); 508*7c478bd9Sstevel@tonic-gate exit(255); 509*7c478bd9Sstevel@tonic-gate } 510*7c478bd9Sstevel@tonic-gate } 511*7c478bd9Sstevel@tonic-gate 512*7c478bd9Sstevel@tonic-gate static int 513*7c478bd9Sstevel@tonic-gate isnumber(char *s) 514*7c478bd9Sstevel@tonic-gate { 515*7c478bd9Sstevel@tonic-gate int c; 516*7c478bd9Sstevel@tonic-gate 517*7c478bd9Sstevel@tonic-gate while ((c = *s++) != '\0') 518*7c478bd9Sstevel@tonic-gate if (!isdigit(c)) 519*7c478bd9Sstevel@tonic-gate return (0); 520*7c478bd9Sstevel@tonic-gate return (1); 521*7c478bd9Sstevel@tonic-gate } 522*7c478bd9Sstevel@tonic-gate 523*7c478bd9Sstevel@tonic-gate 524*7c478bd9Sstevel@tonic-gate static int 525*7c478bd9Sstevel@tonic-gate Perror(char *s) 526*7c478bd9Sstevel@tonic-gate { 527*7c478bd9Sstevel@tonic-gate if (!fflag) { 528*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "chgrp: "); 529*7c478bd9Sstevel@tonic-gate perror(s); 530*7c478bd9Sstevel@tonic-gate } 531*7c478bd9Sstevel@tonic-gate return (!fflag); 532*7c478bd9Sstevel@tonic-gate } 533*7c478bd9Sstevel@tonic-gate 534*7c478bd9Sstevel@tonic-gate 535*7c478bd9Sstevel@tonic-gate static void 536*7c478bd9Sstevel@tonic-gate usage(void) 537*7c478bd9Sstevel@tonic-gate { 538*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 539*7c478bd9Sstevel@tonic-gate "usage:\n" 540*7c478bd9Sstevel@tonic-gate "\tchgrp [-fhR] group file ...\n" 541*7c478bd9Sstevel@tonic-gate "\tchgrp -R [-f] [-H|-L|-P] group file ...\n")); 542*7c478bd9Sstevel@tonic-gate exit(2); 543*7c478bd9Sstevel@tonic-gate } 544