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 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 217c478bd9Sstevel@tonic-gate */ 227c478bd9Sstevel@tonic-gate /* 237c478bd9Sstevel@tonic-gate * Copyright 2003 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 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 * University Copyright- Copyright (c) 1982, 1986, 1988 327c478bd9Sstevel@tonic-gate * The Regents of the University of California 337c478bd9Sstevel@tonic-gate * All Rights Reserved 347c478bd9Sstevel@tonic-gate * 357c478bd9Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from 367c478bd9Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its 377c478bd9Sstevel@tonic-gate * contributors. 387c478bd9Sstevel@tonic-gate */ 397c478bd9Sstevel@tonic-gate 407c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 417c478bd9Sstevel@tonic-gate 427c478bd9Sstevel@tonic-gate /* 437c478bd9Sstevel@tonic-gate * chown [-fR] uid[.gid] file ... 447c478bd9Sstevel@tonic-gate */ 457c478bd9Sstevel@tonic-gate 467c478bd9Sstevel@tonic-gate #include <stdio.h> 477c478bd9Sstevel@tonic-gate #include <ctype.h> 487c478bd9Sstevel@tonic-gate #include <sys/types.h> 497c478bd9Sstevel@tonic-gate #include <sys/stat.h> 507c478bd9Sstevel@tonic-gate #include <pwd.h> 517c478bd9Sstevel@tonic-gate #include <dirent.h> 527c478bd9Sstevel@tonic-gate #include <grp.h> 537c478bd9Sstevel@tonic-gate #include <errno.h> 547c478bd9Sstevel@tonic-gate 557c478bd9Sstevel@tonic-gate struct passwd *pwd; 567c478bd9Sstevel@tonic-gate struct passwd *getpwnam(); 577c478bd9Sstevel@tonic-gate struct stat stbuf; 587c478bd9Sstevel@tonic-gate uid_t uid; 597c478bd9Sstevel@tonic-gate int status; 607c478bd9Sstevel@tonic-gate int fflag; 617c478bd9Sstevel@tonic-gate int rflag; 627c478bd9Sstevel@tonic-gate 63*cc6c5292Schin void fatal(int, char *, char *); 64*cc6c5292Schin 65*cc6c5292Schin int 66*cc6c5292Schin main(int argc, char *argv[]) 677c478bd9Sstevel@tonic-gate { 68*cc6c5292Schin int c; 697c478bd9Sstevel@tonic-gate gid_t gid; 70*cc6c5292Schin char *cp, *group; 71*cc6c5292Schin char optchar[2]; 727c478bd9Sstevel@tonic-gate struct group *grp; 737c478bd9Sstevel@tonic-gate extern char *strchr(); 747c478bd9Sstevel@tonic-gate 757c478bd9Sstevel@tonic-gate argc--, argv++; 767c478bd9Sstevel@tonic-gate while (argc > 0 && argv[0][0] == '-') { 777c478bd9Sstevel@tonic-gate for (cp = &argv[0][1]; *cp; cp++) 787c478bd9Sstevel@tonic-gate 797c478bd9Sstevel@tonic-gate switch (*cp) { 807c478bd9Sstevel@tonic-gate 817c478bd9Sstevel@tonic-gate case 'f': 827c478bd9Sstevel@tonic-gate fflag++; 837c478bd9Sstevel@tonic-gate break; 847c478bd9Sstevel@tonic-gate 857c478bd9Sstevel@tonic-gate case 'R': 867c478bd9Sstevel@tonic-gate rflag++; 877c478bd9Sstevel@tonic-gate break; 887c478bd9Sstevel@tonic-gate 897c478bd9Sstevel@tonic-gate default: 90*cc6c5292Schin optchar[0] = *cp; 91*cc6c5292Schin optchar[1] = '\0'; 92*cc6c5292Schin fatal(255, "unknown option: %s", optchar); 937c478bd9Sstevel@tonic-gate } 947c478bd9Sstevel@tonic-gate argv++, argc--; 957c478bd9Sstevel@tonic-gate } 967c478bd9Sstevel@tonic-gate if (argc < 2) { 977c478bd9Sstevel@tonic-gate fprintf(stderr, "usage: chown [-fR] owner[.group] file ...\n"); 987c478bd9Sstevel@tonic-gate exit(-1); 997c478bd9Sstevel@tonic-gate } 1007c478bd9Sstevel@tonic-gate gid = -1; 1017c478bd9Sstevel@tonic-gate group = strchr(argv[0], '.'); 1027c478bd9Sstevel@tonic-gate if (group != NULL) { 1037c478bd9Sstevel@tonic-gate *group++ = '\0'; 1047c478bd9Sstevel@tonic-gate if (!isnumber(group)) { 1057c478bd9Sstevel@tonic-gate if ((grp = getgrnam(group)) == NULL) 1067c478bd9Sstevel@tonic-gate fatal(255, "unknown group: %s", group); 1077c478bd9Sstevel@tonic-gate gid = grp -> gr_gid; 1087c478bd9Sstevel@tonic-gate (void) endgrent(); 1097c478bd9Sstevel@tonic-gate } else if (*group != '\0') { 1107c478bd9Sstevel@tonic-gate errno = 0; 1117c478bd9Sstevel@tonic-gate gid = (gid_t)strtol(group, NULL, 10); 1127c478bd9Sstevel@tonic-gate if (errno != 0) { 1137c478bd9Sstevel@tonic-gate if (errno == ERANGE) { 1147c478bd9Sstevel@tonic-gate fatal(2, 1157c478bd9Sstevel@tonic-gate "group id too large: %s", group); 1167c478bd9Sstevel@tonic-gate } else { 1177c478bd9Sstevel@tonic-gate fatal(2, "group id invalid: %s", group); 1187c478bd9Sstevel@tonic-gate } 1197c478bd9Sstevel@tonic-gate } 1207c478bd9Sstevel@tonic-gate } 1217c478bd9Sstevel@tonic-gate } 1227c478bd9Sstevel@tonic-gate if (!isnumber(argv[0])) { 1237c478bd9Sstevel@tonic-gate if ((pwd = getpwnam(argv[0])) == NULL) 1247c478bd9Sstevel@tonic-gate fatal(255, "unknown user id: %s", argv[0]); 1257c478bd9Sstevel@tonic-gate uid = pwd->pw_uid; 1267c478bd9Sstevel@tonic-gate } else { 1277c478bd9Sstevel@tonic-gate errno = 0; 1287c478bd9Sstevel@tonic-gate uid = (uid_t)strtol(argv[0], NULL, 10); 1297c478bd9Sstevel@tonic-gate if (errno != 0) { 1307c478bd9Sstevel@tonic-gate if (errno == ERANGE) { 1317c478bd9Sstevel@tonic-gate fatal(2, "user id too large: %s", argv[0]); 1327c478bd9Sstevel@tonic-gate } else { 1337c478bd9Sstevel@tonic-gate fatal(2, "user id invalid: %s", argv[0]); 1347c478bd9Sstevel@tonic-gate } 1357c478bd9Sstevel@tonic-gate } 1367c478bd9Sstevel@tonic-gate } 1377c478bd9Sstevel@tonic-gate for (c = 1; c < argc; c++) { 1387c478bd9Sstevel@tonic-gate /* do stat for directory arguments */ 1397c478bd9Sstevel@tonic-gate if (lstat(argv[c], &stbuf) < 0) { 1407c478bd9Sstevel@tonic-gate status += Perror(argv[c]); 1417c478bd9Sstevel@tonic-gate continue; 1427c478bd9Sstevel@tonic-gate } 1437c478bd9Sstevel@tonic-gate if (rflag && ((stbuf.st_mode&S_IFMT) == S_IFDIR)) { 1447c478bd9Sstevel@tonic-gate status += chownr(argv[c], uid, gid); 1457c478bd9Sstevel@tonic-gate continue; 1467c478bd9Sstevel@tonic-gate } 1477c478bd9Sstevel@tonic-gate if (lchown(argv[c], uid, gid)) { 1487c478bd9Sstevel@tonic-gate status += Perror(argv[c]); 1497c478bd9Sstevel@tonic-gate continue; 1507c478bd9Sstevel@tonic-gate } 1517c478bd9Sstevel@tonic-gate } 152*cc6c5292Schin return (status); 1537c478bd9Sstevel@tonic-gate } 1547c478bd9Sstevel@tonic-gate 155*cc6c5292Schin int 156*cc6c5292Schin isnumber(char *s) 1577c478bd9Sstevel@tonic-gate { 158*cc6c5292Schin int c; 1597c478bd9Sstevel@tonic-gate 1607c478bd9Sstevel@tonic-gate while (c = *s++) 1617c478bd9Sstevel@tonic-gate if (!isdigit(c)) 1627c478bd9Sstevel@tonic-gate return (0); 1637c478bd9Sstevel@tonic-gate return (1); 1647c478bd9Sstevel@tonic-gate } 1657c478bd9Sstevel@tonic-gate 166*cc6c5292Schin int 167*cc6c5292Schin chownr(char *dir, uid_t uid, gid_t gid) 1687c478bd9Sstevel@tonic-gate { 169*cc6c5292Schin DIR *dirp; 170*cc6c5292Schin struct dirent *dp; 1717c478bd9Sstevel@tonic-gate struct stat st; 1727c478bd9Sstevel@tonic-gate char savedir[1024]; 1737c478bd9Sstevel@tonic-gate int ecode; 1747c478bd9Sstevel@tonic-gate 1757c478bd9Sstevel@tonic-gate if (getcwd(savedir, 1024) == NULL) 1767c478bd9Sstevel@tonic-gate fatal(255, "%s", savedir); 1777c478bd9Sstevel@tonic-gate /* 1787c478bd9Sstevel@tonic-gate * Change what we are given before doing it's contents. 1797c478bd9Sstevel@tonic-gate */ 1807c478bd9Sstevel@tonic-gate if (chown(dir, uid, gid) < 0 && Perror(dir)) 1817c478bd9Sstevel@tonic-gate return (1); 1827c478bd9Sstevel@tonic-gate if (chdir(dir) < 0) { 1837c478bd9Sstevel@tonic-gate Perror(dir); 1847c478bd9Sstevel@tonic-gate return (1); 1857c478bd9Sstevel@tonic-gate } 1867c478bd9Sstevel@tonic-gate if ((dirp = opendir(".")) == NULL) { 1877c478bd9Sstevel@tonic-gate Perror(dir); 1887c478bd9Sstevel@tonic-gate return (1); 1897c478bd9Sstevel@tonic-gate } 1907c478bd9Sstevel@tonic-gate dp = readdir(dirp); 1917c478bd9Sstevel@tonic-gate dp = readdir(dirp); /* read "." and ".." */ 1927c478bd9Sstevel@tonic-gate ecode = 0; 1937c478bd9Sstevel@tonic-gate for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) { 1947c478bd9Sstevel@tonic-gate if (lstat(dp->d_name, &st) < 0) { 1957c478bd9Sstevel@tonic-gate ecode = Perror(dp->d_name); 1967c478bd9Sstevel@tonic-gate if (ecode) 1977c478bd9Sstevel@tonic-gate break; 1987c478bd9Sstevel@tonic-gate continue; 1997c478bd9Sstevel@tonic-gate } 2007c478bd9Sstevel@tonic-gate if ((st.st_mode&S_IFMT) == S_IFDIR) { 2017c478bd9Sstevel@tonic-gate ecode = chownr(dp->d_name, uid, gid); 2027c478bd9Sstevel@tonic-gate if (ecode) 2037c478bd9Sstevel@tonic-gate break; 2047c478bd9Sstevel@tonic-gate continue; 2057c478bd9Sstevel@tonic-gate } 2067c478bd9Sstevel@tonic-gate if (lchown(dp->d_name, uid, gid) < 0 && 2077c478bd9Sstevel@tonic-gate (ecode = Perror(dp->d_name))) 2087c478bd9Sstevel@tonic-gate break; 2097c478bd9Sstevel@tonic-gate } 2107c478bd9Sstevel@tonic-gate closedir(dirp); 2117c478bd9Sstevel@tonic-gate if (chdir(savedir) < 0) 2127c478bd9Sstevel@tonic-gate fatal(255, "can't change back to %s", savedir); 2137c478bd9Sstevel@tonic-gate return (ecode); 2147c478bd9Sstevel@tonic-gate } 2157c478bd9Sstevel@tonic-gate 216*cc6c5292Schin int 217*cc6c5292Schin error(char *fmt, char *a) 2187c478bd9Sstevel@tonic-gate { 2197c478bd9Sstevel@tonic-gate 2207c478bd9Sstevel@tonic-gate if (!fflag) { 2217c478bd9Sstevel@tonic-gate fprintf(stderr, "chown: "); 2227c478bd9Sstevel@tonic-gate fprintf(stderr, fmt, a); 2237c478bd9Sstevel@tonic-gate putc('\n', stderr); 2247c478bd9Sstevel@tonic-gate } 2257c478bd9Sstevel@tonic-gate return (!fflag); 2267c478bd9Sstevel@tonic-gate } 2277c478bd9Sstevel@tonic-gate 228*cc6c5292Schin void 229*cc6c5292Schin fatal(int status, char *fmt, char *a) 2307c478bd9Sstevel@tonic-gate { 2317c478bd9Sstevel@tonic-gate 2327c478bd9Sstevel@tonic-gate fflag = 0; 2337c478bd9Sstevel@tonic-gate (void) error(fmt, a); 2347c478bd9Sstevel@tonic-gate exit(status); 2357c478bd9Sstevel@tonic-gate } 2367c478bd9Sstevel@tonic-gate 237*cc6c5292Schin int 238*cc6c5292Schin Perror(char *s) 2397c478bd9Sstevel@tonic-gate { 2407c478bd9Sstevel@tonic-gate 2417c478bd9Sstevel@tonic-gate if (!fflag) { 2427c478bd9Sstevel@tonic-gate fprintf(stderr, "chown: "); 2437c478bd9Sstevel@tonic-gate perror(s); 2447c478bd9Sstevel@tonic-gate } 2457c478bd9Sstevel@tonic-gate return (!fflag); 2467c478bd9Sstevel@tonic-gate } 247