1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
|
/*
* 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
* or http://www.opensolaris.org/os/licensing.
* 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 <sys/types.h>
#include <sys/stat.h>
#include <sys/avl.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 struct group *gr;
static struct stat stbuf;
static struct stat stbuf2;
static gid_t gid;
static int hflag = 0,
fflag = 0,
rflag = 0,
Hflag = 0,
Lflag = 0,
Pflag = 0,
sflag = 0;
static int status = 0; /* total number of errors received */
static avl_tree_t *tree; /* search tree to store inode data */
static void usage(void);
static int isnumber(char *);
static int Perror(char *);
static void chgrpr(char *, gid_t);
#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.
*/
#define FOLLOW_CL_LINKS (rflag)
#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.
*/
#define FOLLOW_CL_LINKS (rflag && (Hflag || Lflag))
#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.
*/
#define FOLLOW_D_LINKS (!Hflag)
#else
/*
* Follow symlinks when traversing directories. Only follow symlinks
* to other parts of the file hierarchy if -L was specified.
*/
#define FOLLOW_D_LINKS (Lflag)
#endif
#define CHOWN(f, u, g) if (chown(f, u, g) < 0) { \
status += Perror(f); \
}
#define LCHOWN(f, u, g) if (lchown(f, u, g) < 0) { \
status += Perror(f); \
}
/*
* We're ignoring errors here because preserving the SET[UG]ID bits is just
* a courtesy. This is only used on directories.
*/
#define SETUGID_PRESERVE(dir, mode) \
if (((mode) & (S_ISGID|S_ISUID)) != 0) \
(void) chmod((dir), (mode) & ~S_IFMT)
extern int optind;
int
main(int argc, char *argv[])
{
int c;
/* set the locale for only the messages system (all else is clean) */
(void) setlocale(LC_ALL, "");
#if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
#define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */
#endif
(void) textdomain(TEXT_DOMAIN);
while ((c = getopt(argc, argv, "RhfHLPs")) != EOF)
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.
*/
Lflag = Pflag = 0;
Hflag++;
break;
case 'L':
Hflag = Pflag = 0;
Lflag++;
break;
case 'P':
Hflag = Lflag = 0;
Pflag++;
break;
case 's':
sflag++;
break;
default:
usage();
}
/*
* Check for sufficient arguments
* or a usage error.
*/
argc -= optind;
argv = &argv[optind];
if ((argc < 2) ||
((Hflag || Lflag || Pflag) && !rflag) ||
((Hflag || Lflag || Pflag) && hflag)) {
usage();
}
if (sflag) {
if (sid_to_id(argv[0], B_FALSE, &gid)) {
(void) fprintf(stderr, gettext(
"chgrp: invalid group sid %s\n"), argv[0]);
exit(2);
}
} else if ((gr = getgrnam(argv[0])) != NULL) {
gid = gr->gr_gid;
} else {
if (isnumber(argv[0])) {
errno = 0;
/* gid is an int */
gid = (gid_t)strtoul(argv[0], NULL, 10);
if (errno != 0) {
if (errno == ERANGE) {
(void) fprintf(stderr, gettext(
"chgrp: group id is too large\n"));
exit(2);
} else {
(void) fprintf(stderr, gettext(
"chgrp: invalid group id\n"));
exit(2);
}
}
} else {
(void) fprintf(stderr, "chgrp: ");
(void) fprintf(stderr, gettext("unknown group: %s\n"),
argv[0]);
exit(2);
}
}
for (c = 1; c < argc; c++) {
tree = NULL;
if (lstat(argv[c], &stbuf) < 0) {
status += Perror(argv[c]);
continue;
}
if (rflag && ((stbuf.st_mode & S_IFMT) == S_IFLNK)) {
if (hflag || Pflag) {
/*
* 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.
*/
LCHOWN(argv[c], -1, gid);
} else {
if (stat(argv[c], &stbuf2) < 0) {
status += Perror(argv[c]);
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) {
if ((stbuf2.st_mode & S_IFMT)
== 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.
*/
if (add_tnode(&tree,
stbuf2.st_dev,
stbuf2.st_ino) == 1) {
chgrpr(argv[c], gid);
/*
* Try to restore the
* SET[UG]ID bits.
*/
SETUGID_PRESERVE(
argv[c],
stbuf2.st_mode &
~S_IFMT);
} else {
/*
* Error occurred.
* rc can't be 0
* as this is the first
* node to be added to
* the search tree.
*/
status += Perror(
argv[c]);
}
} else {
/*
* Change the group id of the
* file referenced by the
* symbolic link.
*/
CHOWN(argv[c], -1, gid);
}
} else {
/*
* Change the group id of the file
* referenced by the symbolic link.
*/
CHOWN(argv[c], -1, gid);
if ((stbuf2.st_mode & S_IFMT)
== S_IFDIR) {
/* Reset the SET[UG]ID bits. */
SETUGID_PRESERVE(argv[c],
stbuf2.st_mode & ~S_IFMT);
}
}
}
} else if (rflag && ((stbuf.st_mode & S_IFMT) == S_IFDIR)) {
/*
* Add this node to the search tree so we don't
* get into a endless loop.
*/
if (add_tnode(&tree, stbuf.st_dev,
stbuf.st_ino) == 1) {
chgrpr(argv[c], gid);
/* Restore the SET[UG]ID bits. */
SETUGID_PRESERVE(argv[c],
stbuf.st_mode & ~S_IFMT);
} 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.
*/
status += Perror(argv[c]);
}
} else {
if (hflag || Pflag) {
LCHOWN(argv[c], -1, gid);
} else {
CHOWN(argv[c], -1, gid);
}
/* If a directory, reset the SET[UG]ID bits. */
if ((stbuf.st_mode & S_IFMT) == S_IFDIR) {
SETUGID_PRESERVE(argv[c],
stbuf.st_mode & ~S_IFMT);
}
}
}
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
chgrpr(char *dir, gid_t gid)
{
struct dirent *dp;
DIR *dirp;
struct stat st, st2;
char savedir[1024];
if (getcwd(savedir, 1024) == 0) {
(void) fprintf(stderr, "chgrp: ");
(void) fprintf(stderr, gettext("%s\n"), savedir);
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'.
*/
CHOWN(dir, -1, gid);
if (chdir(dir) < 0) {
status += Perror(dir);
return;
}
if ((dirp = opendir(".")) == NULL) {
status += Perror(dir);
return;
}
for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) {
if ((strcmp(dp->d_name, ".") == 0) ||
(strcmp(dp->d_name, "..") == 0)) {
continue; /* skip "." and ".." */
}
if (lstat(dp->d_name, &st) < 0) {
status += Perror(dp->d_name);
continue;
}
if ((st.st_mode & S_IFMT) == S_IFLNK) {
if (hflag || Pflag) {
/*
* 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.
*/
LCHOWN(dp->d_name, -1, gid);
} else {
if (stat(dp->d_name, &st2) < 0) {
status += Perror(dp->d_name);
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) {
if ((st2.st_mode & S_IFMT) == 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.
*/
int rc;
if ((rc = add_tnode(&tree,
st2.st_dev,
st2.st_ino)) == 1) {
chgrpr(dp->d_name, gid);
/*
* Restore SET[UG]ID
* bits.
*/
SETUGID_PRESERVE(
dp->d_name,
st2.st_mode &
~S_IFMT);
} else if (rc == 0) {
/* already visited */
continue;
} else {
/*
* An error occurred
* while trying to add
* the node to the tree.
*/
status += Perror(
dp->d_name);
continue;
}
} else {
/*
* Change the group id of the
* file referenced by the
* symbolic link.
*/
CHOWN(dp->d_name, -1, gid);
}
} else {
/*
* Change the group id of the file
* referenced by the symbolic link.
*/
CHOWN(dp->d_name, -1, gid);
if ((st2.st_mode & S_IFMT) == S_IFDIR) {
/* Restore SET[UG]ID bits. */
SETUGID_PRESERVE(dp->d_name,
st2.st_mode & ~S_IFMT);
}
}
}
} else if ((st.st_mode & S_IFMT) == S_IFDIR) {
/*
* Add this node to the search tree so we don't
* get into a endless loop.
*/
int rc;
if ((rc = add_tnode(&tree, st.st_dev,
st.st_ino)) == 1) {
chgrpr(dp->d_name, gid);
/* Restore the SET[UG]ID bits. */
SETUGID_PRESERVE(dp->d_name,
st.st_mode & ~S_IFMT);
} else if (rc == 0) {
/* already visited */
continue;
} else {
/*
* An error occurred while trying
* to add the node to the search tree.
*/
status += Perror(dp->d_name);
continue;
}
} else {
CHOWN(dp->d_name, -1, gid);
}
}
(void) closedir(dirp);
if (chdir(savedir) < 0) {
(void) fprintf(stderr, "chgrp: ");
(void) fprintf(stderr, gettext("can't change back to %s\n"),
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) {
(void) fprintf(stderr, "chgrp: ");
perror(s);
}
return (!fflag);
}
static void
usage(void)
{
(void) fprintf(stderr, gettext(
"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);
}
|