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 5c4518760Scasper * Common Development and Distribution License (the "License"). 6c4518760Scasper * 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 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */ 277c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate /* 307c478bd9Sstevel@tonic-gate * Portions of this source code were derived from Berkeley 4.3 BSD 317c478bd9Sstevel@tonic-gate * under license from the Regents of the University of California. 327c478bd9Sstevel@tonic-gate */ 337c478bd9Sstevel@tonic-gate 347c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 357c478bd9Sstevel@tonic-gate 367c478bd9Sstevel@tonic-gate /* 377c478bd9Sstevel@tonic-gate * chown [-fhR] uid[:gid] file ... 387c478bd9Sstevel@tonic-gate * chown -R [-f] [-H|-L|-P] uid[:gid] file ... 39*b249c65cSmarks * chown -s [-fhR] ownersid[:groupsid] file ... 40*b249c65cSmarks * chown -s -R [-f] [-H|-L|-P] ownersid[:groupsid] file ... 417c478bd9Sstevel@tonic-gate */ 427c478bd9Sstevel@tonic-gate 437c478bd9Sstevel@tonic-gate #include <stdio.h> 447c478bd9Sstevel@tonic-gate #include <stdlib.h> 457c478bd9Sstevel@tonic-gate #include <ctype.h> 467c478bd9Sstevel@tonic-gate #include <sys/types.h> 477c478bd9Sstevel@tonic-gate #include <dirent.h> 487c478bd9Sstevel@tonic-gate #include <string.h> 497c478bd9Sstevel@tonic-gate #include <sys/stat.h> 507c478bd9Sstevel@tonic-gate #include <sys/avl.h> 517c478bd9Sstevel@tonic-gate #include <pwd.h> 527c478bd9Sstevel@tonic-gate #include <grp.h> 537c478bd9Sstevel@tonic-gate #include <unistd.h> 547c478bd9Sstevel@tonic-gate #include <locale.h> 557c478bd9Sstevel@tonic-gate #include <errno.h> 567c478bd9Sstevel@tonic-gate #include <libcmdutils.h> 57*b249c65cSmarks #include <aclutils.h> 587c478bd9Sstevel@tonic-gate 597c478bd9Sstevel@tonic-gate static struct passwd *pwd; 607c478bd9Sstevel@tonic-gate static struct group *grp; 617c478bd9Sstevel@tonic-gate static struct stat stbuf; 62f48205beScasper static uid_t uid = (uid_t)-1; 63f48205beScasper static gid_t gid = (gid_t)-1; 647c478bd9Sstevel@tonic-gate static int status = 0; /* total number of errors received */ 657c478bd9Sstevel@tonic-gate static int hflag = 0, 667c478bd9Sstevel@tonic-gate rflag = 0, 677c478bd9Sstevel@tonic-gate fflag = 0, 687c478bd9Sstevel@tonic-gate Hflag = 0, 697c478bd9Sstevel@tonic-gate Lflag = 0, 70*b249c65cSmarks Pflag = 0, 71*b249c65cSmarks sflag = 0; 727c478bd9Sstevel@tonic-gate static avl_tree_t *tree; 737c478bd9Sstevel@tonic-gate 747c478bd9Sstevel@tonic-gate static int Perror(char *); 757c478bd9Sstevel@tonic-gate static int isnumber(char *); 767c478bd9Sstevel@tonic-gate static void chownr(char *, uid_t, gid_t); 777c478bd9Sstevel@tonic-gate static void usage(); 787c478bd9Sstevel@tonic-gate 797c478bd9Sstevel@tonic-gate #ifdef XPG4 807c478bd9Sstevel@tonic-gate /* 817c478bd9Sstevel@tonic-gate * Check to see if we are to follow symlinks specified on the command line. 827c478bd9Sstevel@tonic-gate * This assumes we've already checked to make sure neither -h or -P was 837c478bd9Sstevel@tonic-gate * specified, so we are just looking to see if -R -H, or -R -L was specified, 847c478bd9Sstevel@tonic-gate * or, since -R has the same behavior as -R -L, if -R was specified by itself. 857c478bd9Sstevel@tonic-gate * Therefore, all we really need to check for is if -R was specified. 867c478bd9Sstevel@tonic-gate */ 877c478bd9Sstevel@tonic-gate #define FOLLOW_CL_LINKS (rflag) 887c478bd9Sstevel@tonic-gate #else 897c478bd9Sstevel@tonic-gate /* 907c478bd9Sstevel@tonic-gate * Check to see if we are to follow symlinks specified on the command line. 917c478bd9Sstevel@tonic-gate * This assumes we've already checked to make sure neither -h or -P was 927c478bd9Sstevel@tonic-gate * specified, so we are just looking to see if -R -H, or -R -L was specified. 937c478bd9Sstevel@tonic-gate * Note: -R by itself will change the ownership of a directory referenced by a 947c478bd9Sstevel@tonic-gate * symlink however it will now follow the symlink to any other part of the 957c478bd9Sstevel@tonic-gate * file hierarchy. 967c478bd9Sstevel@tonic-gate */ 977c478bd9Sstevel@tonic-gate #define FOLLOW_CL_LINKS (rflag && (Hflag || Lflag)) 987c478bd9Sstevel@tonic-gate #endif 997c478bd9Sstevel@tonic-gate 1007c478bd9Sstevel@tonic-gate #ifdef XPG4 1017c478bd9Sstevel@tonic-gate /* 1027c478bd9Sstevel@tonic-gate * Follow symlinks when traversing directories. Since -R behaves the 1037c478bd9Sstevel@tonic-gate * same as -R -L, we always want to follow symlinks to other parts 1047c478bd9Sstevel@tonic-gate * of the file hierarchy unless -H was specified. 1057c478bd9Sstevel@tonic-gate */ 1067c478bd9Sstevel@tonic-gate #define FOLLOW_D_LINKS (!Hflag) 1077c478bd9Sstevel@tonic-gate #else 1087c478bd9Sstevel@tonic-gate /* 1097c478bd9Sstevel@tonic-gate * Follow symlinks when traversing directories. Only follow symlinks 1107c478bd9Sstevel@tonic-gate * to other parts of the file hierarchy if -L was specified. 1117c478bd9Sstevel@tonic-gate */ 1127c478bd9Sstevel@tonic-gate #define FOLLOW_D_LINKS (Lflag) 1137c478bd9Sstevel@tonic-gate #endif 1147c478bd9Sstevel@tonic-gate 1157c478bd9Sstevel@tonic-gate #define CHOWN(f, u, g) if (chown(f, u, g) < 0) { \ 1167c478bd9Sstevel@tonic-gate status += Perror(f); \ 1177c478bd9Sstevel@tonic-gate } 1187c478bd9Sstevel@tonic-gate #define LCHOWN(f, u, g) if (lchown(f, u, g) < 0) { \ 1197c478bd9Sstevel@tonic-gate status += Perror(f); \ 1207c478bd9Sstevel@tonic-gate } 1217c478bd9Sstevel@tonic-gate 1227c478bd9Sstevel@tonic-gate 1237c478bd9Sstevel@tonic-gate int 1247c478bd9Sstevel@tonic-gate main(int argc, char *argv[]) 1257c478bd9Sstevel@tonic-gate { 1267c478bd9Sstevel@tonic-gate int c; 1277c478bd9Sstevel@tonic-gate int ch; 1287c478bd9Sstevel@tonic-gate char *grpp; /* pointer to group name arg */ 1297c478bd9Sstevel@tonic-gate extern int optind; 1307c478bd9Sstevel@tonic-gate int errflg = 0; 1317c478bd9Sstevel@tonic-gate 1327c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 1337c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */ 1347c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */ 1357c478bd9Sstevel@tonic-gate #endif 1367c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 1377c478bd9Sstevel@tonic-gate 138*b249c65cSmarks while ((ch = getopt(argc, argv, "hRfHLPs")) != EOF) { 1397c478bd9Sstevel@tonic-gate switch (ch) { 1407c478bd9Sstevel@tonic-gate case 'h': 1417c478bd9Sstevel@tonic-gate hflag++; 1427c478bd9Sstevel@tonic-gate break; 1437c478bd9Sstevel@tonic-gate 1447c478bd9Sstevel@tonic-gate case 'R': 1457c478bd9Sstevel@tonic-gate rflag++; 1467c478bd9Sstevel@tonic-gate break; 1477c478bd9Sstevel@tonic-gate 1487c478bd9Sstevel@tonic-gate case 'f': 1497c478bd9Sstevel@tonic-gate fflag++; 1507c478bd9Sstevel@tonic-gate break; 1517c478bd9Sstevel@tonic-gate 1527c478bd9Sstevel@tonic-gate case 'H': 1537c478bd9Sstevel@tonic-gate /* 1547c478bd9Sstevel@tonic-gate * If more than one of -H, -L, and -P 1557c478bd9Sstevel@tonic-gate * are specified, only the last option 1567c478bd9Sstevel@tonic-gate * specified determines the behavior of 1577c478bd9Sstevel@tonic-gate * chown. 1587c478bd9Sstevel@tonic-gate */ 1597c478bd9Sstevel@tonic-gate Lflag = Pflag = 0; 1607c478bd9Sstevel@tonic-gate Hflag++; 1617c478bd9Sstevel@tonic-gate break; 1627c478bd9Sstevel@tonic-gate 1637c478bd9Sstevel@tonic-gate case 'L': 1647c478bd9Sstevel@tonic-gate Hflag = Pflag = 0; 1657c478bd9Sstevel@tonic-gate Lflag++; 1667c478bd9Sstevel@tonic-gate break; 1677c478bd9Sstevel@tonic-gate 1687c478bd9Sstevel@tonic-gate case 'P': 1697c478bd9Sstevel@tonic-gate Hflag = Lflag = 0; 1707c478bd9Sstevel@tonic-gate Pflag++; 1717c478bd9Sstevel@tonic-gate break; 1727c478bd9Sstevel@tonic-gate 173*b249c65cSmarks case 's': 174*b249c65cSmarks sflag++; 175*b249c65cSmarks break; 176*b249c65cSmarks 1777c478bd9Sstevel@tonic-gate default: 1787c478bd9Sstevel@tonic-gate errflg++; 1797c478bd9Sstevel@tonic-gate break; 1807c478bd9Sstevel@tonic-gate } 1817c478bd9Sstevel@tonic-gate } 1827c478bd9Sstevel@tonic-gate /* 1837c478bd9Sstevel@tonic-gate * Check for sufficient arguments 1847c478bd9Sstevel@tonic-gate * or a usage error. 1857c478bd9Sstevel@tonic-gate */ 1867c478bd9Sstevel@tonic-gate 1877c478bd9Sstevel@tonic-gate argc -= optind; 1887c478bd9Sstevel@tonic-gate argv = &argv[optind]; 1897c478bd9Sstevel@tonic-gate 1907c478bd9Sstevel@tonic-gate if (errflg || (argc < 2) || 1917c478bd9Sstevel@tonic-gate ((Hflag || Lflag || Pflag) && !rflag) || 1927c478bd9Sstevel@tonic-gate ((Hflag || Lflag || Pflag) && hflag)) { 1937c478bd9Sstevel@tonic-gate usage(); 1947c478bd9Sstevel@tonic-gate } 1957c478bd9Sstevel@tonic-gate 1967c478bd9Sstevel@tonic-gate /* 1977c478bd9Sstevel@tonic-gate * POSIX.2 1987c478bd9Sstevel@tonic-gate * Check for owner[:group] 1997c478bd9Sstevel@tonic-gate */ 2007c478bd9Sstevel@tonic-gate if ((grpp = strchr(argv[0], ':')) != NULL) { 2017c478bd9Sstevel@tonic-gate *grpp++ = 0; 202*b249c65cSmarks 203*b249c65cSmarks if (sflag) { 204*b249c65cSmarks if (sid_to_id(grpp, B_FALSE, &gid)) { 205*b249c65cSmarks (void) fprintf(stderr, gettext( 206*b249c65cSmarks "chown: invalid owning group sid %s\n"), 207*b249c65cSmarks grpp); 208*b249c65cSmarks exit(2); 209*b249c65cSmarks } 210*b249c65cSmarks } else if ((grp = getgrnam(grpp)) != NULL) { 211bda8495cSas145665 gid = grp->gr_gid; 212bda8495cSas145665 } else { 2137c478bd9Sstevel@tonic-gate if (isnumber(grpp)) { 2147c478bd9Sstevel@tonic-gate errno = 0; 215f48205beScasper gid = (gid_t)strtoul(grpp, NULL, 10); 2167c478bd9Sstevel@tonic-gate if (errno != 0) { 2177c478bd9Sstevel@tonic-gate if (errno == ERANGE) { 2187c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 2197c478bd9Sstevel@tonic-gate "chown: group id too large\n")); 2207c478bd9Sstevel@tonic-gate exit(2); 2217c478bd9Sstevel@tonic-gate } else { 2227c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 2237c478bd9Sstevel@tonic-gate "chown: invalid group id\n")); 2247c478bd9Sstevel@tonic-gate exit(2); 2257c478bd9Sstevel@tonic-gate } 2267c478bd9Sstevel@tonic-gate } 227bda8495cSas145665 } else { 2287c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 2297c478bd9Sstevel@tonic-gate "chown: unknown group id %s\n"), grpp); 2307c478bd9Sstevel@tonic-gate exit(2); 231bda8495cSas145665 } 232bda8495cSas145665 } 2337c478bd9Sstevel@tonic-gate } 2347c478bd9Sstevel@tonic-gate 235*b249c65cSmarks if (sflag) { 236*b249c65cSmarks if (sid_to_id(argv[0], B_TRUE, &uid)) { 237*b249c65cSmarks (void) fprintf(stderr, gettext( 238*b249c65cSmarks "chown: invalid owner sid %s\n"), argv[0]); 239*b249c65cSmarks exit(2); 240*b249c65cSmarks } 241*b249c65cSmarks } else if ((pwd = getpwnam(argv[0])) != NULL) { 242bda8495cSas145665 uid = pwd->pw_uid; 243bda8495cSas145665 } else { 2447c478bd9Sstevel@tonic-gate if (isnumber(argv[0])) { 2457c478bd9Sstevel@tonic-gate errno = 0; 246f48205beScasper uid = (uid_t)strtoul(argv[0], NULL, 10); 2477c478bd9Sstevel@tonic-gate if (errno != 0) { 2487c478bd9Sstevel@tonic-gate if (errno == ERANGE) { 2497c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 2507c478bd9Sstevel@tonic-gate "chown: user id too large\n")); 2517c478bd9Sstevel@tonic-gate exit(2); 2527c478bd9Sstevel@tonic-gate } else { 2537c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 2547c478bd9Sstevel@tonic-gate "chown: invalid user id\n")); 2557c478bd9Sstevel@tonic-gate exit(2); 2567c478bd9Sstevel@tonic-gate } 2577c478bd9Sstevel@tonic-gate } 258bda8495cSas145665 } else { 2597c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 2607c478bd9Sstevel@tonic-gate "chown: unknown user id %s\n"), argv[0]); 2617c478bd9Sstevel@tonic-gate exit(2); 262bda8495cSas145665 } 263bda8495cSas145665 } 2647c478bd9Sstevel@tonic-gate 2657c478bd9Sstevel@tonic-gate for (c = 1; c < argc; c++) { 2667c478bd9Sstevel@tonic-gate tree = NULL; 2677c478bd9Sstevel@tonic-gate if (lstat(argv[c], &stbuf) < 0) { 2687c478bd9Sstevel@tonic-gate status += Perror(argv[c]); 2697c478bd9Sstevel@tonic-gate continue; 2707c478bd9Sstevel@tonic-gate } 2717c478bd9Sstevel@tonic-gate if (rflag && ((stbuf.st_mode & S_IFMT) == S_IFLNK)) { 2727c478bd9Sstevel@tonic-gate if (hflag || Pflag) { 2737c478bd9Sstevel@tonic-gate /* 2747c478bd9Sstevel@tonic-gate * Change the ownership of the symlink 2757c478bd9Sstevel@tonic-gate * specified on the command line. 2767c478bd9Sstevel@tonic-gate * Don't follow the symbolic link to 2777c478bd9Sstevel@tonic-gate * any other part of the file hierarchy. 2787c478bd9Sstevel@tonic-gate */ 2797c478bd9Sstevel@tonic-gate LCHOWN(argv[c], uid, gid); 2807c478bd9Sstevel@tonic-gate } else { 2817c478bd9Sstevel@tonic-gate struct stat stbuf2; 2827c478bd9Sstevel@tonic-gate if (stat(argv[c], &stbuf2) < 0) { 2837c478bd9Sstevel@tonic-gate status += Perror(argv[c]); 2847c478bd9Sstevel@tonic-gate continue; 2857c478bd9Sstevel@tonic-gate } 2867c478bd9Sstevel@tonic-gate /* 2877c478bd9Sstevel@tonic-gate * We know that we are to change the 2887c478bd9Sstevel@tonic-gate * ownership of the file referenced by the 2897c478bd9Sstevel@tonic-gate * symlink specified on the command line. 2907c478bd9Sstevel@tonic-gate * Now check to see if we are to follow 2917c478bd9Sstevel@tonic-gate * the symlink to any other part of the 2927c478bd9Sstevel@tonic-gate * file hierarchy. 2937c478bd9Sstevel@tonic-gate */ 2947c478bd9Sstevel@tonic-gate if (FOLLOW_CL_LINKS) { 2957c478bd9Sstevel@tonic-gate if ((stbuf2.st_mode & S_IFMT) 2967c478bd9Sstevel@tonic-gate == S_IFDIR) { 2977c478bd9Sstevel@tonic-gate /* 2987c478bd9Sstevel@tonic-gate * We are following symlinks so 2997c478bd9Sstevel@tonic-gate * traverse into the directory. 3007c478bd9Sstevel@tonic-gate * Add this node to the search 3017c478bd9Sstevel@tonic-gate * tree so we don't get into an 3027c478bd9Sstevel@tonic-gate * endless loop. 3037c478bd9Sstevel@tonic-gate */ 3047c478bd9Sstevel@tonic-gate if (add_tnode(&tree, 3057c478bd9Sstevel@tonic-gate stbuf2.st_dev, 3067c478bd9Sstevel@tonic-gate stbuf2.st_ino) == 1) { 3077c478bd9Sstevel@tonic-gate chownr(argv[c], 3087c478bd9Sstevel@tonic-gate uid, gid); 3097c478bd9Sstevel@tonic-gate } else { 3107c478bd9Sstevel@tonic-gate /* 3117c478bd9Sstevel@tonic-gate * Error occurred. 3127c478bd9Sstevel@tonic-gate * rc can't be 0 3137c478bd9Sstevel@tonic-gate * as this is the first 3147c478bd9Sstevel@tonic-gate * node to be added to 3157c478bd9Sstevel@tonic-gate * the search tree. 3167c478bd9Sstevel@tonic-gate */ 3177c478bd9Sstevel@tonic-gate status += Perror( 3187c478bd9Sstevel@tonic-gate argv[c]); 3197c478bd9Sstevel@tonic-gate } 3207c478bd9Sstevel@tonic-gate } else { 3217c478bd9Sstevel@tonic-gate /* 3227c478bd9Sstevel@tonic-gate * Change the user ID of the 3237c478bd9Sstevel@tonic-gate * file referenced by the 3247c478bd9Sstevel@tonic-gate * symlink. 3257c478bd9Sstevel@tonic-gate */ 3267c478bd9Sstevel@tonic-gate CHOWN(argv[c], uid, gid); 3277c478bd9Sstevel@tonic-gate } 3287c478bd9Sstevel@tonic-gate } else { 3297c478bd9Sstevel@tonic-gate /* 3307c478bd9Sstevel@tonic-gate * Change the user ID of the file 3317c478bd9Sstevel@tonic-gate * referenced by the symbolic link. 3327c478bd9Sstevel@tonic-gate */ 3337c478bd9Sstevel@tonic-gate CHOWN(argv[c], uid, gid); 3347c478bd9Sstevel@tonic-gate } 3357c478bd9Sstevel@tonic-gate } 3367c478bd9Sstevel@tonic-gate } else if (rflag && ((stbuf.st_mode & S_IFMT) == S_IFDIR)) { 3377c478bd9Sstevel@tonic-gate /* 3387c478bd9Sstevel@tonic-gate * Add this node to the search tree so we don't 3397c478bd9Sstevel@tonic-gate * get into a endless loop. 3407c478bd9Sstevel@tonic-gate */ 3417c478bd9Sstevel@tonic-gate if (add_tnode(&tree, stbuf.st_dev, 3427c478bd9Sstevel@tonic-gate stbuf.st_ino) == 1) { 3437c478bd9Sstevel@tonic-gate chownr(argv[c], uid, gid); 3447c478bd9Sstevel@tonic-gate } else { 3457c478bd9Sstevel@tonic-gate /* 3467c478bd9Sstevel@tonic-gate * An error occurred while trying 3477c478bd9Sstevel@tonic-gate * to add the node to the tree. 3487c478bd9Sstevel@tonic-gate * Continue on with next file 3497c478bd9Sstevel@tonic-gate * specified. Note: rc shouldn't 3507c478bd9Sstevel@tonic-gate * be 0 as this was the first node 3517c478bd9Sstevel@tonic-gate * being added to the search tree. 3527c478bd9Sstevel@tonic-gate */ 3537c478bd9Sstevel@tonic-gate status += Perror(argv[c]); 3547c478bd9Sstevel@tonic-gate } 3557c478bd9Sstevel@tonic-gate } else if (hflag || Pflag) { 3567c478bd9Sstevel@tonic-gate LCHOWN(argv[c], uid, gid); 3577c478bd9Sstevel@tonic-gate } else { 3587c478bd9Sstevel@tonic-gate CHOWN(argv[c], uid, gid); 3597c478bd9Sstevel@tonic-gate } 3607c478bd9Sstevel@tonic-gate } 3617c478bd9Sstevel@tonic-gate return (status); 3627c478bd9Sstevel@tonic-gate } 3637c478bd9Sstevel@tonic-gate 3647c478bd9Sstevel@tonic-gate /* 3657c478bd9Sstevel@tonic-gate * chownr() - recursive chown() 3667c478bd9Sstevel@tonic-gate * 3677c478bd9Sstevel@tonic-gate * Recursively chowns the input directory then its contents. rflag must 3687c478bd9Sstevel@tonic-gate * have been set if chownr() is called. The input directory should not 3697c478bd9Sstevel@tonic-gate * be a sym link (this is handled in the calling routine). In 3707c478bd9Sstevel@tonic-gate * addition, the calling routine should have already added the input 3717c478bd9Sstevel@tonic-gate * directory to the search tree so we do not get into endless loops. 3727c478bd9Sstevel@tonic-gate * Note: chownr() doesn't need a return value as errors are reported 3737c478bd9Sstevel@tonic-gate * through the global "status" variable. 3747c478bd9Sstevel@tonic-gate */ 3757c478bd9Sstevel@tonic-gate static void 3767c478bd9Sstevel@tonic-gate chownr(char *dir, uid_t uid, gid_t gid) 3777c478bd9Sstevel@tonic-gate { 3787c478bd9Sstevel@tonic-gate DIR *dirp; 3797c478bd9Sstevel@tonic-gate struct dirent *dp; 3807c478bd9Sstevel@tonic-gate struct stat st, st2; 3817c478bd9Sstevel@tonic-gate char savedir[1024]; 3827c478bd9Sstevel@tonic-gate 3837c478bd9Sstevel@tonic-gate if (getcwd(savedir, 1024) == (char *)0) { 3847c478bd9Sstevel@tonic-gate (void) Perror("getcwd"); 3857c478bd9Sstevel@tonic-gate exit(255); 3867c478bd9Sstevel@tonic-gate } 3877c478bd9Sstevel@tonic-gate 3887c478bd9Sstevel@tonic-gate /* 3897c478bd9Sstevel@tonic-gate * Attempt to chown the directory, however don't return if we 3907c478bd9Sstevel@tonic-gate * can't as we still may be able to chown the contents of the 3917c478bd9Sstevel@tonic-gate * directory. Note: the calling routine resets the SUID bits 3927c478bd9Sstevel@tonic-gate * on this directory so we don't have to perform an extra 'stat'. 3937c478bd9Sstevel@tonic-gate */ 3947c478bd9Sstevel@tonic-gate CHOWN(dir, uid, gid); 3957c478bd9Sstevel@tonic-gate 3967c478bd9Sstevel@tonic-gate if (chdir(dir) < 0) { 3977c478bd9Sstevel@tonic-gate status += Perror(dir); 3987c478bd9Sstevel@tonic-gate return; 3997c478bd9Sstevel@tonic-gate } 4007c478bd9Sstevel@tonic-gate if ((dirp = opendir(".")) == NULL) { 4017c478bd9Sstevel@tonic-gate status += Perror(dir); 4027c478bd9Sstevel@tonic-gate return; 4037c478bd9Sstevel@tonic-gate } 4047c478bd9Sstevel@tonic-gate for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) { 405c4518760Scasper if (strcmp(dp->d_name, ".") == 0 || /* skip . and .. */ 406c4518760Scasper strcmp(dp->d_name, "..") == 0) { 407c4518760Scasper continue; 408c4518760Scasper } 4097c478bd9Sstevel@tonic-gate if (lstat(dp->d_name, &st) < 0) { 4107c478bd9Sstevel@tonic-gate status += Perror(dp->d_name); 4117c478bd9Sstevel@tonic-gate continue; 4127c478bd9Sstevel@tonic-gate } 4137c478bd9Sstevel@tonic-gate if ((st.st_mode & S_IFMT) == S_IFLNK) { 4147c478bd9Sstevel@tonic-gate if (hflag || Pflag) { 4157c478bd9Sstevel@tonic-gate /* 4167c478bd9Sstevel@tonic-gate * Change the ownership of the symbolic link 4177c478bd9Sstevel@tonic-gate * encountered while traversing the 4187c478bd9Sstevel@tonic-gate * directory. Don't follow the symbolic 4197c478bd9Sstevel@tonic-gate * link to any other part of the file 4207c478bd9Sstevel@tonic-gate * hierarchy. 4217c478bd9Sstevel@tonic-gate */ 4227c478bd9Sstevel@tonic-gate LCHOWN(dp->d_name, uid, gid); 4237c478bd9Sstevel@tonic-gate } else { 4247c478bd9Sstevel@tonic-gate if (stat(dp->d_name, &st2) < 0) { 4257c478bd9Sstevel@tonic-gate status += Perror(dp->d_name); 4267c478bd9Sstevel@tonic-gate continue; 4277c478bd9Sstevel@tonic-gate } 4287c478bd9Sstevel@tonic-gate /* 4297c478bd9Sstevel@tonic-gate * We know that we are to change the 4307c478bd9Sstevel@tonic-gate * ownership of the file referenced by the 4317c478bd9Sstevel@tonic-gate * symlink encountered while traversing 4327c478bd9Sstevel@tonic-gate * the directory. Now check to see if we 4337c478bd9Sstevel@tonic-gate * are to follow the symlink to any other 4347c478bd9Sstevel@tonic-gate * part of the file hierarchy. 4357c478bd9Sstevel@tonic-gate */ 4367c478bd9Sstevel@tonic-gate if (FOLLOW_D_LINKS) { 4377c478bd9Sstevel@tonic-gate if ((st2.st_mode & S_IFMT) == S_IFDIR) { 4387c478bd9Sstevel@tonic-gate /* 4397c478bd9Sstevel@tonic-gate * We are following symlinks so 4407c478bd9Sstevel@tonic-gate * traverse into the directory. 4417c478bd9Sstevel@tonic-gate * Add this node to the search 4427c478bd9Sstevel@tonic-gate * tree so we don't get into an 4437c478bd9Sstevel@tonic-gate * endless loop. 4447c478bd9Sstevel@tonic-gate */ 4457c478bd9Sstevel@tonic-gate int rc; 4467c478bd9Sstevel@tonic-gate if ((rc = add_tnode(&tree, 4477c478bd9Sstevel@tonic-gate st2.st_dev, 4487c478bd9Sstevel@tonic-gate st2.st_ino)) == 1) { 4497c478bd9Sstevel@tonic-gate chownr(dp->d_name, 4507c478bd9Sstevel@tonic-gate uid, gid); 4517c478bd9Sstevel@tonic-gate } else if (rc == 0) { 4527c478bd9Sstevel@tonic-gate /* already visited */ 4537c478bd9Sstevel@tonic-gate continue; 4547c478bd9Sstevel@tonic-gate } else { 4557c478bd9Sstevel@tonic-gate /* 4567c478bd9Sstevel@tonic-gate * An error occurred 4577c478bd9Sstevel@tonic-gate * while trying to add 4587c478bd9Sstevel@tonic-gate * the node to the tree. 4597c478bd9Sstevel@tonic-gate */ 4607c478bd9Sstevel@tonic-gate status += Perror( 4617c478bd9Sstevel@tonic-gate dp->d_name); 4627c478bd9Sstevel@tonic-gate continue; 4637c478bd9Sstevel@tonic-gate } 4647c478bd9Sstevel@tonic-gate } else { 4657c478bd9Sstevel@tonic-gate /* 4667c478bd9Sstevel@tonic-gate * Change the user id of the 4677c478bd9Sstevel@tonic-gate * file referenced by the 4687c478bd9Sstevel@tonic-gate * symbolic link. 4697c478bd9Sstevel@tonic-gate */ 4707c478bd9Sstevel@tonic-gate CHOWN(dp->d_name, uid, gid); 4717c478bd9Sstevel@tonic-gate } 4727c478bd9Sstevel@tonic-gate } else { 4737c478bd9Sstevel@tonic-gate /* 4747c478bd9Sstevel@tonic-gate * Change the user id of the file 4757c478bd9Sstevel@tonic-gate * referenced by the symbolic link. 4767c478bd9Sstevel@tonic-gate */ 4777c478bd9Sstevel@tonic-gate CHOWN(dp->d_name, uid, gid); 4787c478bd9Sstevel@tonic-gate } 4797c478bd9Sstevel@tonic-gate } 4807c478bd9Sstevel@tonic-gate } else if ((st.st_mode & S_IFMT) == S_IFDIR) { 4817c478bd9Sstevel@tonic-gate /* 4827c478bd9Sstevel@tonic-gate * Add this node to the search tree so we don't 4837c478bd9Sstevel@tonic-gate * get into a endless loop. 4847c478bd9Sstevel@tonic-gate */ 4857c478bd9Sstevel@tonic-gate int rc; 4867c478bd9Sstevel@tonic-gate if ((rc = add_tnode(&tree, st.st_dev, 4877c478bd9Sstevel@tonic-gate st.st_ino)) == 1) { 4887c478bd9Sstevel@tonic-gate chownr(dp->d_name, uid, gid); 4897c478bd9Sstevel@tonic-gate } else if (rc == 0) { 4907c478bd9Sstevel@tonic-gate /* already visited */ 4917c478bd9Sstevel@tonic-gate continue; 4927c478bd9Sstevel@tonic-gate } else { 4937c478bd9Sstevel@tonic-gate /* 4947c478bd9Sstevel@tonic-gate * An error occurred while trying 4957c478bd9Sstevel@tonic-gate * to add the node to the search tree. 4967c478bd9Sstevel@tonic-gate */ 4977c478bd9Sstevel@tonic-gate status += Perror(dp->d_name); 4987c478bd9Sstevel@tonic-gate continue; 4997c478bd9Sstevel@tonic-gate } 5007c478bd9Sstevel@tonic-gate } else { 5017c478bd9Sstevel@tonic-gate CHOWN(dp->d_name, uid, gid); 5027c478bd9Sstevel@tonic-gate } 5037c478bd9Sstevel@tonic-gate } 5047c478bd9Sstevel@tonic-gate 5057c478bd9Sstevel@tonic-gate (void) closedir(dirp); 5067c478bd9Sstevel@tonic-gate if (chdir(savedir) < 0) { 5077c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 5087c478bd9Sstevel@tonic-gate "chown: can't change back to %s\n"), savedir); 5097c478bd9Sstevel@tonic-gate exit(255); 5107c478bd9Sstevel@tonic-gate } 5117c478bd9Sstevel@tonic-gate } 5127c478bd9Sstevel@tonic-gate 5137c478bd9Sstevel@tonic-gate static int 5147c478bd9Sstevel@tonic-gate isnumber(char *s) 5157c478bd9Sstevel@tonic-gate { 5167c478bd9Sstevel@tonic-gate int c; 5177c478bd9Sstevel@tonic-gate 5187c478bd9Sstevel@tonic-gate while ((c = *s++) != '\0') 5197c478bd9Sstevel@tonic-gate if (!isdigit(c)) 5207c478bd9Sstevel@tonic-gate return (0); 5217c478bd9Sstevel@tonic-gate return (1); 5227c478bd9Sstevel@tonic-gate } 5237c478bd9Sstevel@tonic-gate 5247c478bd9Sstevel@tonic-gate static int 5257c478bd9Sstevel@tonic-gate Perror(char *s) 5267c478bd9Sstevel@tonic-gate { 5277c478bd9Sstevel@tonic-gate if (!fflag) { 5287c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "chown: "); 5297c478bd9Sstevel@tonic-gate perror(s); 5307c478bd9Sstevel@tonic-gate } 5317c478bd9Sstevel@tonic-gate return (!fflag); 5327c478bd9Sstevel@tonic-gate } 5337c478bd9Sstevel@tonic-gate 5347c478bd9Sstevel@tonic-gate static void 5357c478bd9Sstevel@tonic-gate usage() 5367c478bd9Sstevel@tonic-gate { 5377c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 5387c478bd9Sstevel@tonic-gate "usage:\n" 5397c478bd9Sstevel@tonic-gate "\tchown [-fhR] owner[:group] file...\n" 540*b249c65cSmarks "\tchown -R [-f] [-H|-L|-P] owner[:group] file...\n" 541*b249c65cSmarks "\tchown -s [-fhR] ownersid[:groupsid] file...\n" 542*b249c65cSmarks "\tchown -s -R [-f] [-H|-L|-P] ownersid[:groupsid] file...\n")); 5437c478bd9Sstevel@tonic-gate exit(2); 5447c478bd9Sstevel@tonic-gate } 545