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