xref: /titanic_44/usr/src/cmd/chown/chown.c (revision f48205be61a214698b763ff550ab9e657525104c)
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*f48205beScasper  * Copyright 2007 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 ...
397c478bd9Sstevel@tonic-gate  */
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #include <stdio.h>
427c478bd9Sstevel@tonic-gate #include <stdlib.h>
437c478bd9Sstevel@tonic-gate #include <ctype.h>
447c478bd9Sstevel@tonic-gate #include <sys/types.h>
457c478bd9Sstevel@tonic-gate #include <dirent.h>
467c478bd9Sstevel@tonic-gate #include <string.h>
477c478bd9Sstevel@tonic-gate #include <sys/stat.h>
487c478bd9Sstevel@tonic-gate #include <sys/avl.h>
497c478bd9Sstevel@tonic-gate #include <pwd.h>
507c478bd9Sstevel@tonic-gate #include <grp.h>
517c478bd9Sstevel@tonic-gate #include <unistd.h>
527c478bd9Sstevel@tonic-gate #include <locale.h>
537c478bd9Sstevel@tonic-gate #include <errno.h>
547c478bd9Sstevel@tonic-gate #include <libcmdutils.h>
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate static struct		passwd	*pwd;
577c478bd9Sstevel@tonic-gate static struct		group	*grp;
587c478bd9Sstevel@tonic-gate static struct		stat	stbuf;
59*f48205beScasper static uid_t		uid = (uid_t)-1;
60*f48205beScasper static gid_t		gid = (gid_t)-1;
617c478bd9Sstevel@tonic-gate static int		status = 0;	/* total number of errors received */
627c478bd9Sstevel@tonic-gate static int		hflag = 0,
637c478bd9Sstevel@tonic-gate 			rflag = 0,
647c478bd9Sstevel@tonic-gate 			fflag = 0,
657c478bd9Sstevel@tonic-gate 			Hflag = 0,
667c478bd9Sstevel@tonic-gate 			Lflag = 0,
677c478bd9Sstevel@tonic-gate 			Pflag = 0;
687c478bd9Sstevel@tonic-gate static avl_tree_t	*tree;
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate static int		Perror(char *);
717c478bd9Sstevel@tonic-gate static int		isnumber(char *);
727c478bd9Sstevel@tonic-gate static void		chownr(char *, uid_t, gid_t);
737c478bd9Sstevel@tonic-gate static void		usage();
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate #ifdef XPG4
767c478bd9Sstevel@tonic-gate /*
777c478bd9Sstevel@tonic-gate  * Check to see if we are to follow symlinks specified on the command line.
787c478bd9Sstevel@tonic-gate  * This assumes we've already checked to make sure neither -h or -P was
797c478bd9Sstevel@tonic-gate  * specified, so we are just looking to see if -R -H, or -R -L was specified,
807c478bd9Sstevel@tonic-gate  * or, since -R has the same behavior as -R -L, if -R was specified by itself.
817c478bd9Sstevel@tonic-gate  * Therefore, all we really need to check for is if -R was specified.
827c478bd9Sstevel@tonic-gate  */
837c478bd9Sstevel@tonic-gate #define	FOLLOW_CL_LINKS	(rflag)
847c478bd9Sstevel@tonic-gate #else
857c478bd9Sstevel@tonic-gate /*
867c478bd9Sstevel@tonic-gate  * Check to see if we are to follow symlinks specified on the command line.
877c478bd9Sstevel@tonic-gate  * This assumes we've already checked to make sure neither -h or -P was
887c478bd9Sstevel@tonic-gate  * specified, so we are just looking to see if -R -H, or -R -L was specified.
897c478bd9Sstevel@tonic-gate  * Note: -R by itself will change the ownership of a directory referenced by a
907c478bd9Sstevel@tonic-gate  * symlink however it will now follow the symlink to any other part of the
917c478bd9Sstevel@tonic-gate  * file hierarchy.
927c478bd9Sstevel@tonic-gate  */
937c478bd9Sstevel@tonic-gate #define	FOLLOW_CL_LINKS	(rflag && (Hflag || Lflag))
947c478bd9Sstevel@tonic-gate #endif
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate #ifdef XPG4
977c478bd9Sstevel@tonic-gate /*
987c478bd9Sstevel@tonic-gate  * Follow symlinks when traversing directories.  Since -R behaves the
997c478bd9Sstevel@tonic-gate  * same as -R -L, we always want to follow symlinks to other parts
1007c478bd9Sstevel@tonic-gate  * of the file hierarchy unless -H was specified.
1017c478bd9Sstevel@tonic-gate  */
1027c478bd9Sstevel@tonic-gate #define	FOLLOW_D_LINKS	(!Hflag)
1037c478bd9Sstevel@tonic-gate #else
1047c478bd9Sstevel@tonic-gate /*
1057c478bd9Sstevel@tonic-gate  * Follow symlinks when traversing directories.  Only follow symlinks
1067c478bd9Sstevel@tonic-gate  * to other parts of the file hierarchy if -L was specified.
1077c478bd9Sstevel@tonic-gate  */
1087c478bd9Sstevel@tonic-gate #define	FOLLOW_D_LINKS	(Lflag)
1097c478bd9Sstevel@tonic-gate #endif
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate #define	CHOWN(f, u, g)	if (chown(f, u, g) < 0) { \
1127c478bd9Sstevel@tonic-gate 				status += Perror(f); \
1137c478bd9Sstevel@tonic-gate 			}
1147c478bd9Sstevel@tonic-gate #define	LCHOWN(f, u, g)	if (lchown(f, u, g) < 0) { \
1157c478bd9Sstevel@tonic-gate 				status += Perror(f); \
1167c478bd9Sstevel@tonic-gate 			}
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate int
1207c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
1217c478bd9Sstevel@tonic-gate {
1227c478bd9Sstevel@tonic-gate 	int c;
1237c478bd9Sstevel@tonic-gate 	int ch;
1247c478bd9Sstevel@tonic-gate 	char *grpp;			/* pointer to group name arg */
1257c478bd9Sstevel@tonic-gate 	extern int optind;
1267c478bd9Sstevel@tonic-gate 	int errflg = 0;
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1297c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)		/* Should be defined by cc -D */
1307c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"	/* Use this only if it weren't */
1317c478bd9Sstevel@tonic-gate #endif
1327c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	while ((ch = getopt(argc, argv, "hRfHLP")) != EOF) {
1357c478bd9Sstevel@tonic-gate 		switch (ch) {
1367c478bd9Sstevel@tonic-gate 		case 'h':
1377c478bd9Sstevel@tonic-gate 			hflag++;
1387c478bd9Sstevel@tonic-gate 			break;
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 		case 'R':
1417c478bd9Sstevel@tonic-gate 			rflag++;
1427c478bd9Sstevel@tonic-gate 			break;
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 		case 'f':
1457c478bd9Sstevel@tonic-gate 			fflag++;
1467c478bd9Sstevel@tonic-gate 			break;
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 		case 'H':
1497c478bd9Sstevel@tonic-gate 			/*
1507c478bd9Sstevel@tonic-gate 			 * If more than one of -H, -L, and -P
1517c478bd9Sstevel@tonic-gate 			 * are specified, only the last option
1527c478bd9Sstevel@tonic-gate 			 * specified determines the behavior of
1537c478bd9Sstevel@tonic-gate 			 * chown.
1547c478bd9Sstevel@tonic-gate 			 */
1557c478bd9Sstevel@tonic-gate 			Lflag = Pflag = 0;
1567c478bd9Sstevel@tonic-gate 			Hflag++;
1577c478bd9Sstevel@tonic-gate 			break;
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 		case 'L':
1607c478bd9Sstevel@tonic-gate 			Hflag = Pflag = 0;
1617c478bd9Sstevel@tonic-gate 			Lflag++;
1627c478bd9Sstevel@tonic-gate 			break;
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 		case 'P':
1657c478bd9Sstevel@tonic-gate 			Hflag = Lflag = 0;
1667c478bd9Sstevel@tonic-gate 			Pflag++;
1677c478bd9Sstevel@tonic-gate 			break;
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 		default:
1707c478bd9Sstevel@tonic-gate 			errflg++;
1717c478bd9Sstevel@tonic-gate 			break;
1727c478bd9Sstevel@tonic-gate 		}
1737c478bd9Sstevel@tonic-gate 	}
1747c478bd9Sstevel@tonic-gate 	/*
1757c478bd9Sstevel@tonic-gate 	 * Check for sufficient arguments
1767c478bd9Sstevel@tonic-gate 	 * or a usage error.
1777c478bd9Sstevel@tonic-gate 	 */
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	argc -= optind;
1807c478bd9Sstevel@tonic-gate 	argv = &argv[optind];
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	if (errflg || (argc < 2) ||
1837c478bd9Sstevel@tonic-gate 	    ((Hflag || Lflag || Pflag) && !rflag) ||
1847c478bd9Sstevel@tonic-gate 	    ((Hflag || Lflag || Pflag) && hflag)) {
1857c478bd9Sstevel@tonic-gate 		usage();
1867c478bd9Sstevel@tonic-gate 	}
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	/*
1897c478bd9Sstevel@tonic-gate 	 * POSIX.2
1907c478bd9Sstevel@tonic-gate 	 * Check for owner[:group]
1917c478bd9Sstevel@tonic-gate 	 */
1927c478bd9Sstevel@tonic-gate 	if ((grpp = strchr(argv[0], ':')) != NULL) {
1937c478bd9Sstevel@tonic-gate 		*grpp++ = 0;
194bda8495cSas145665 		if ((grp = getgrnam(grpp)) != NULL) {
195bda8495cSas145665 			gid = grp->gr_gid;
196bda8495cSas145665 		} else {
1977c478bd9Sstevel@tonic-gate 			if (isnumber(grpp)) {
1987c478bd9Sstevel@tonic-gate 				errno = 0;
199*f48205beScasper 				gid = (gid_t)strtoul(grpp, NULL, 10);
2007c478bd9Sstevel@tonic-gate 				if (errno != 0) {
2017c478bd9Sstevel@tonic-gate 					if (errno == ERANGE) {
2027c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr, gettext(
2037c478bd9Sstevel@tonic-gate 						"chown: group id too large\n"));
2047c478bd9Sstevel@tonic-gate 							exit(2);
2057c478bd9Sstevel@tonic-gate 					} else {
2067c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr, gettext(
2077c478bd9Sstevel@tonic-gate 						"chown: invalid group id\n"));
2087c478bd9Sstevel@tonic-gate 						exit(2);
2097c478bd9Sstevel@tonic-gate 					}
2107c478bd9Sstevel@tonic-gate 				}
211bda8495cSas145665 			} else {
2127c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
2137c478bd9Sstevel@tonic-gate 					"chown: unknown group id %s\n"), grpp);
2147c478bd9Sstevel@tonic-gate 				exit(2);
215bda8495cSas145665 			}
216bda8495cSas145665 		}
2177c478bd9Sstevel@tonic-gate 	}
2187c478bd9Sstevel@tonic-gate 
219bda8495cSas145665 	if ((pwd = getpwnam(argv[0])) != NULL) {
220bda8495cSas145665 		uid = pwd->pw_uid;
221bda8495cSas145665 	} else {
2227c478bd9Sstevel@tonic-gate 		if (isnumber(argv[0])) {
2237c478bd9Sstevel@tonic-gate 			errno = 0;
224*f48205beScasper 			uid = (uid_t)strtoul(argv[0], NULL, 10);
2257c478bd9Sstevel@tonic-gate 			if (errno != 0) {
2267c478bd9Sstevel@tonic-gate 				if (errno == ERANGE) {
2277c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
2287c478bd9Sstevel@tonic-gate 					"chown: user id too large\n"));
2297c478bd9Sstevel@tonic-gate 					exit(2);
2307c478bd9Sstevel@tonic-gate 				} else {
2317c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
2327c478bd9Sstevel@tonic-gate 					"chown: invalid user id\n"));
2337c478bd9Sstevel@tonic-gate 					exit(2);
2347c478bd9Sstevel@tonic-gate 				}
2357c478bd9Sstevel@tonic-gate 			}
236bda8495cSas145665 		} else {
2377c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
2387c478bd9Sstevel@tonic-gate 			"chown: unknown user id %s\n"), argv[0]);
2397c478bd9Sstevel@tonic-gate 			exit(2);
240bda8495cSas145665 		}
241bda8495cSas145665 	}
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 	for (c = 1; c < argc; c++) {
2447c478bd9Sstevel@tonic-gate 		tree = NULL;
2457c478bd9Sstevel@tonic-gate 		if (lstat(argv[c], &stbuf) < 0) {
2467c478bd9Sstevel@tonic-gate 			status += Perror(argv[c]);
2477c478bd9Sstevel@tonic-gate 			continue;
2487c478bd9Sstevel@tonic-gate 		}
2497c478bd9Sstevel@tonic-gate 		if (rflag && ((stbuf.st_mode & S_IFMT) == S_IFLNK)) {
2507c478bd9Sstevel@tonic-gate 			if (hflag || Pflag) {
2517c478bd9Sstevel@tonic-gate 				/*
2527c478bd9Sstevel@tonic-gate 				 * Change the ownership of the symlink
2537c478bd9Sstevel@tonic-gate 				 * specified on the command line.
2547c478bd9Sstevel@tonic-gate 				 * Don't follow the symbolic link to
2557c478bd9Sstevel@tonic-gate 				 * any other part of the file hierarchy.
2567c478bd9Sstevel@tonic-gate 				 */
2577c478bd9Sstevel@tonic-gate 				LCHOWN(argv[c], uid, gid);
2587c478bd9Sstevel@tonic-gate 			} else {
2597c478bd9Sstevel@tonic-gate 				struct stat stbuf2;
2607c478bd9Sstevel@tonic-gate 				if (stat(argv[c], &stbuf2) < 0) {
2617c478bd9Sstevel@tonic-gate 					status += Perror(argv[c]);
2627c478bd9Sstevel@tonic-gate 					continue;
2637c478bd9Sstevel@tonic-gate 				}
2647c478bd9Sstevel@tonic-gate 				/*
2657c478bd9Sstevel@tonic-gate 				 * We know that we are to change the
2667c478bd9Sstevel@tonic-gate 				 * ownership of the file referenced by the
2677c478bd9Sstevel@tonic-gate 				 * symlink specified on the command line.
2687c478bd9Sstevel@tonic-gate 				 * Now check to see if we are to follow
2697c478bd9Sstevel@tonic-gate 				 * the symlink to any other part of the
2707c478bd9Sstevel@tonic-gate 				 * file hierarchy.
2717c478bd9Sstevel@tonic-gate 				 */
2727c478bd9Sstevel@tonic-gate 				if (FOLLOW_CL_LINKS) {
2737c478bd9Sstevel@tonic-gate 					if ((stbuf2.st_mode & S_IFMT)
2747c478bd9Sstevel@tonic-gate 					    == S_IFDIR) {
2757c478bd9Sstevel@tonic-gate 						/*
2767c478bd9Sstevel@tonic-gate 						 * We are following symlinks so
2777c478bd9Sstevel@tonic-gate 						 * traverse into the directory.
2787c478bd9Sstevel@tonic-gate 						 * Add this node to the search
2797c478bd9Sstevel@tonic-gate 						 * tree so we don't get into an
2807c478bd9Sstevel@tonic-gate 						 * endless loop.
2817c478bd9Sstevel@tonic-gate 						 */
2827c478bd9Sstevel@tonic-gate 						if (add_tnode(&tree,
2837c478bd9Sstevel@tonic-gate 						    stbuf2.st_dev,
2847c478bd9Sstevel@tonic-gate 						    stbuf2.st_ino) == 1) {
2857c478bd9Sstevel@tonic-gate 							chownr(argv[c],
2867c478bd9Sstevel@tonic-gate 							    uid, gid);
2877c478bd9Sstevel@tonic-gate 						} else {
2887c478bd9Sstevel@tonic-gate 							/*
2897c478bd9Sstevel@tonic-gate 							 * Error occurred.
2907c478bd9Sstevel@tonic-gate 							 * rc can't be 0
2917c478bd9Sstevel@tonic-gate 							 * as this is the first
2927c478bd9Sstevel@tonic-gate 							 * node to be added to
2937c478bd9Sstevel@tonic-gate 							 * the search tree.
2947c478bd9Sstevel@tonic-gate 							 */
2957c478bd9Sstevel@tonic-gate 							status += Perror(
2967c478bd9Sstevel@tonic-gate 							    argv[c]);
2977c478bd9Sstevel@tonic-gate 						}
2987c478bd9Sstevel@tonic-gate 					} else {
2997c478bd9Sstevel@tonic-gate 						/*
3007c478bd9Sstevel@tonic-gate 						 * Change the user ID of the
3017c478bd9Sstevel@tonic-gate 						 * file referenced by the
3027c478bd9Sstevel@tonic-gate 						 * symlink.
3037c478bd9Sstevel@tonic-gate 						 */
3047c478bd9Sstevel@tonic-gate 						CHOWN(argv[c], uid, gid);
3057c478bd9Sstevel@tonic-gate 					}
3067c478bd9Sstevel@tonic-gate 				} else {
3077c478bd9Sstevel@tonic-gate 					/*
3087c478bd9Sstevel@tonic-gate 					 * Change the user ID of the file
3097c478bd9Sstevel@tonic-gate 					 * referenced by the symbolic link.
3107c478bd9Sstevel@tonic-gate 					 */
3117c478bd9Sstevel@tonic-gate 					CHOWN(argv[c], uid, gid);
3127c478bd9Sstevel@tonic-gate 				}
3137c478bd9Sstevel@tonic-gate 			}
3147c478bd9Sstevel@tonic-gate 		} else if (rflag && ((stbuf.st_mode & S_IFMT) == S_IFDIR)) {
3157c478bd9Sstevel@tonic-gate 			/*
3167c478bd9Sstevel@tonic-gate 			 * Add this node to the search tree so we don't
3177c478bd9Sstevel@tonic-gate 			 * get into a endless loop.
3187c478bd9Sstevel@tonic-gate 			 */
3197c478bd9Sstevel@tonic-gate 			if (add_tnode(&tree, stbuf.st_dev,
3207c478bd9Sstevel@tonic-gate 			    stbuf.st_ino) == 1) {
3217c478bd9Sstevel@tonic-gate 				chownr(argv[c], uid, gid);
3227c478bd9Sstevel@tonic-gate 			} else {
3237c478bd9Sstevel@tonic-gate 				/*
3247c478bd9Sstevel@tonic-gate 				 * An error occurred while trying
3257c478bd9Sstevel@tonic-gate 				 * to add the node to the tree.
3267c478bd9Sstevel@tonic-gate 				 * Continue on with next file
3277c478bd9Sstevel@tonic-gate 				 * specified.  Note: rc shouldn't
3287c478bd9Sstevel@tonic-gate 				 * be 0 as this was the first node
3297c478bd9Sstevel@tonic-gate 				 * being added to the search tree.
3307c478bd9Sstevel@tonic-gate 				 */
3317c478bd9Sstevel@tonic-gate 				status += Perror(argv[c]);
3327c478bd9Sstevel@tonic-gate 			}
3337c478bd9Sstevel@tonic-gate 		} else if (hflag || Pflag) {
3347c478bd9Sstevel@tonic-gate 			LCHOWN(argv[c], uid, gid);
3357c478bd9Sstevel@tonic-gate 		} else {
3367c478bd9Sstevel@tonic-gate 			CHOWN(argv[c], uid, gid);
3377c478bd9Sstevel@tonic-gate 		}
3387c478bd9Sstevel@tonic-gate 	}
3397c478bd9Sstevel@tonic-gate 	return (status);
3407c478bd9Sstevel@tonic-gate }
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate /*
3437c478bd9Sstevel@tonic-gate  * chownr() - recursive chown()
3447c478bd9Sstevel@tonic-gate  *
3457c478bd9Sstevel@tonic-gate  * Recursively chowns the input directory then its contents.  rflag must
3467c478bd9Sstevel@tonic-gate  * have been set if chownr() is called.  The input directory should not
3477c478bd9Sstevel@tonic-gate  * be a sym link (this is handled in the calling routine).  In
3487c478bd9Sstevel@tonic-gate  * addition, the calling routine should have already added the input
3497c478bd9Sstevel@tonic-gate  * directory to the search tree so we do not get into endless loops.
3507c478bd9Sstevel@tonic-gate  * Note: chownr() doesn't need a return value as errors are reported
3517c478bd9Sstevel@tonic-gate  * through the global "status" variable.
3527c478bd9Sstevel@tonic-gate  */
3537c478bd9Sstevel@tonic-gate static void
3547c478bd9Sstevel@tonic-gate chownr(char *dir, uid_t uid, gid_t gid)
3557c478bd9Sstevel@tonic-gate {
3567c478bd9Sstevel@tonic-gate 	DIR *dirp;
3577c478bd9Sstevel@tonic-gate 	struct dirent *dp;
3587c478bd9Sstevel@tonic-gate 	struct stat st, st2;
3597c478bd9Sstevel@tonic-gate 	char savedir[1024];
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	if (getcwd(savedir, 1024) == (char *)0) {
3627c478bd9Sstevel@tonic-gate 		(void) Perror("getcwd");
3637c478bd9Sstevel@tonic-gate 		exit(255);
3647c478bd9Sstevel@tonic-gate 	}
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 	/*
3677c478bd9Sstevel@tonic-gate 	 * Attempt to chown the directory, however don't return if we
3687c478bd9Sstevel@tonic-gate 	 * can't as we still may be able to chown the contents of the
3697c478bd9Sstevel@tonic-gate 	 * directory.  Note: the calling routine resets the SUID bits
3707c478bd9Sstevel@tonic-gate 	 * on this directory so we don't have to perform an extra 'stat'.
3717c478bd9Sstevel@tonic-gate 	 */
3727c478bd9Sstevel@tonic-gate 	CHOWN(dir, uid, gid);
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	if (chdir(dir) < 0) {
3757c478bd9Sstevel@tonic-gate 		status += Perror(dir);
3767c478bd9Sstevel@tonic-gate 		return;
3777c478bd9Sstevel@tonic-gate 	}
3787c478bd9Sstevel@tonic-gate 	if ((dirp = opendir(".")) == NULL) {
3797c478bd9Sstevel@tonic-gate 		status += Perror(dir);
3807c478bd9Sstevel@tonic-gate 		return;
3817c478bd9Sstevel@tonic-gate 	}
3827c478bd9Sstevel@tonic-gate 	for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) {
383c4518760Scasper 		if (strcmp(dp->d_name, ".") == 0 ||	/* skip . and .. */
384c4518760Scasper 		    strcmp(dp->d_name, "..") == 0) {
385c4518760Scasper 			continue;
386c4518760Scasper 		}
3877c478bd9Sstevel@tonic-gate 		if (lstat(dp->d_name, &st) < 0) {
3887c478bd9Sstevel@tonic-gate 			status += Perror(dp->d_name);
3897c478bd9Sstevel@tonic-gate 			continue;
3907c478bd9Sstevel@tonic-gate 		}
3917c478bd9Sstevel@tonic-gate 		if ((st.st_mode & S_IFMT) == S_IFLNK) {
3927c478bd9Sstevel@tonic-gate 			if (hflag || Pflag) {
3937c478bd9Sstevel@tonic-gate 				/*
3947c478bd9Sstevel@tonic-gate 				 * Change the ownership of the symbolic link
3957c478bd9Sstevel@tonic-gate 				 * encountered while traversing the
3967c478bd9Sstevel@tonic-gate 				 * directory.  Don't follow the symbolic
3977c478bd9Sstevel@tonic-gate 				 * link to any other part of the file
3987c478bd9Sstevel@tonic-gate 				 * hierarchy.
3997c478bd9Sstevel@tonic-gate 				 */
4007c478bd9Sstevel@tonic-gate 				LCHOWN(dp->d_name, uid, gid);
4017c478bd9Sstevel@tonic-gate 			} else {
4027c478bd9Sstevel@tonic-gate 				if (stat(dp->d_name, &st2) < 0) {
4037c478bd9Sstevel@tonic-gate 					status += Perror(dp->d_name);
4047c478bd9Sstevel@tonic-gate 					continue;
4057c478bd9Sstevel@tonic-gate 				}
4067c478bd9Sstevel@tonic-gate 				/*
4077c478bd9Sstevel@tonic-gate 				 * We know that we are to change the
4087c478bd9Sstevel@tonic-gate 				 * ownership of the file referenced by the
4097c478bd9Sstevel@tonic-gate 				 * symlink encountered while traversing
4107c478bd9Sstevel@tonic-gate 				 * the directory.  Now check to see if we
4117c478bd9Sstevel@tonic-gate 				 * are to follow the symlink to any other
4127c478bd9Sstevel@tonic-gate 				 * part of the file hierarchy.
4137c478bd9Sstevel@tonic-gate 				 */
4147c478bd9Sstevel@tonic-gate 				if (FOLLOW_D_LINKS) {
4157c478bd9Sstevel@tonic-gate 					if ((st2.st_mode & S_IFMT) == S_IFDIR) {
4167c478bd9Sstevel@tonic-gate 						/*
4177c478bd9Sstevel@tonic-gate 						 * We are following symlinks so
4187c478bd9Sstevel@tonic-gate 						 * traverse into the directory.
4197c478bd9Sstevel@tonic-gate 						 * Add this node to the search
4207c478bd9Sstevel@tonic-gate 						 * tree so we don't get into an
4217c478bd9Sstevel@tonic-gate 						 * endless loop.
4227c478bd9Sstevel@tonic-gate 						 */
4237c478bd9Sstevel@tonic-gate 						int rc;
4247c478bd9Sstevel@tonic-gate 						if ((rc = add_tnode(&tree,
4257c478bd9Sstevel@tonic-gate 						    st2.st_dev,
4267c478bd9Sstevel@tonic-gate 						    st2.st_ino)) == 1) {
4277c478bd9Sstevel@tonic-gate 							chownr(dp->d_name,
4287c478bd9Sstevel@tonic-gate 							    uid, gid);
4297c478bd9Sstevel@tonic-gate 						} else if (rc == 0) {
4307c478bd9Sstevel@tonic-gate 							/* already visited */
4317c478bd9Sstevel@tonic-gate 							continue;
4327c478bd9Sstevel@tonic-gate 						} else {
4337c478bd9Sstevel@tonic-gate 							/*
4347c478bd9Sstevel@tonic-gate 							 * An error occurred
4357c478bd9Sstevel@tonic-gate 							 * while trying to add
4367c478bd9Sstevel@tonic-gate 							 * the node to the tree.
4377c478bd9Sstevel@tonic-gate 							 */
4387c478bd9Sstevel@tonic-gate 							status += Perror(
4397c478bd9Sstevel@tonic-gate 							    dp->d_name);
4407c478bd9Sstevel@tonic-gate 							continue;
4417c478bd9Sstevel@tonic-gate 						}
4427c478bd9Sstevel@tonic-gate 					} else {
4437c478bd9Sstevel@tonic-gate 						/*
4447c478bd9Sstevel@tonic-gate 						 * Change the user id of the
4457c478bd9Sstevel@tonic-gate 						 * file referenced by the
4467c478bd9Sstevel@tonic-gate 						 * symbolic link.
4477c478bd9Sstevel@tonic-gate 						 */
4487c478bd9Sstevel@tonic-gate 						CHOWN(dp->d_name, uid, gid);
4497c478bd9Sstevel@tonic-gate 					}
4507c478bd9Sstevel@tonic-gate 				} else {
4517c478bd9Sstevel@tonic-gate 					/*
4527c478bd9Sstevel@tonic-gate 					 * Change the user id of the file
4537c478bd9Sstevel@tonic-gate 					 * referenced by the symbolic link.
4547c478bd9Sstevel@tonic-gate 					 */
4557c478bd9Sstevel@tonic-gate 					CHOWN(dp->d_name, uid, gid);
4567c478bd9Sstevel@tonic-gate 				}
4577c478bd9Sstevel@tonic-gate 			}
4587c478bd9Sstevel@tonic-gate 		} else if ((st.st_mode & S_IFMT) == S_IFDIR) {
4597c478bd9Sstevel@tonic-gate 			/*
4607c478bd9Sstevel@tonic-gate 			 * Add this node to the search tree so we don't
4617c478bd9Sstevel@tonic-gate 			 * get into a endless loop.
4627c478bd9Sstevel@tonic-gate 			 */
4637c478bd9Sstevel@tonic-gate 			int rc;
4647c478bd9Sstevel@tonic-gate 			if ((rc = add_tnode(&tree, st.st_dev,
4657c478bd9Sstevel@tonic-gate 			    st.st_ino)) == 1) {
4667c478bd9Sstevel@tonic-gate 				chownr(dp->d_name, uid, gid);
4677c478bd9Sstevel@tonic-gate 			} else if (rc == 0) {
4687c478bd9Sstevel@tonic-gate 				/* already visited */
4697c478bd9Sstevel@tonic-gate 				continue;
4707c478bd9Sstevel@tonic-gate 			} else {
4717c478bd9Sstevel@tonic-gate 				/*
4727c478bd9Sstevel@tonic-gate 				 * An error occurred while trying
4737c478bd9Sstevel@tonic-gate 				 * to add the node to the search tree.
4747c478bd9Sstevel@tonic-gate 				 */
4757c478bd9Sstevel@tonic-gate 				status += Perror(dp->d_name);
4767c478bd9Sstevel@tonic-gate 				continue;
4777c478bd9Sstevel@tonic-gate 			}
4787c478bd9Sstevel@tonic-gate 		} else {
4797c478bd9Sstevel@tonic-gate 			CHOWN(dp->d_name, uid, gid);
4807c478bd9Sstevel@tonic-gate 		}
4817c478bd9Sstevel@tonic-gate 	}
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 	(void) closedir(dirp);
4847c478bd9Sstevel@tonic-gate 	if (chdir(savedir) < 0) {
4857c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
4867c478bd9Sstevel@tonic-gate 			"chown: can't change back to %s\n"), savedir);
4877c478bd9Sstevel@tonic-gate 		exit(255);
4887c478bd9Sstevel@tonic-gate 	}
4897c478bd9Sstevel@tonic-gate }
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate static int
4927c478bd9Sstevel@tonic-gate isnumber(char *s)
4937c478bd9Sstevel@tonic-gate {
4947c478bd9Sstevel@tonic-gate 	int c;
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 	while ((c = *s++) != '\0')
4977c478bd9Sstevel@tonic-gate 		if (!isdigit(c))
4987c478bd9Sstevel@tonic-gate 			return (0);
4997c478bd9Sstevel@tonic-gate 	return (1);
5007c478bd9Sstevel@tonic-gate }
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate static int
5037c478bd9Sstevel@tonic-gate Perror(char *s)
5047c478bd9Sstevel@tonic-gate {
5057c478bd9Sstevel@tonic-gate 	if (!fflag) {
5067c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "chown: ");
5077c478bd9Sstevel@tonic-gate 		perror(s);
5087c478bd9Sstevel@tonic-gate 	}
5097c478bd9Sstevel@tonic-gate 	return (!fflag);
5107c478bd9Sstevel@tonic-gate }
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate static void
5137c478bd9Sstevel@tonic-gate usage()
5147c478bd9Sstevel@tonic-gate {
5157c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
5167c478bd9Sstevel@tonic-gate 	    "usage:\n"
5177c478bd9Sstevel@tonic-gate 	    "\tchown [-fhR] owner[:group] file...\n"
5187c478bd9Sstevel@tonic-gate 	    "\tchown -R [-f] [-H|-L|-P] owner[:group] file...\n"));
5197c478bd9Sstevel@tonic-gate 	exit(2);
5207c478bd9Sstevel@tonic-gate }
521