xref: /titanic_41/usr/src/uts/common/syscall/chown.c (revision 4202ea4b139fb1fab45cd14d9d03a81b86902341)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
27 /*	  All Rights Reserved  	*/
28 
29 /*
30  * Portions of this source code were derived from Berkeley 4.3 BSD
31  * under license from the Regents of the University of California.
32  */
33 
34 #pragma ident	"%Z%%M%	%I%	%E% SMI"
35 
36 #include <sys/param.h>
37 #include <sys/isa_defs.h>
38 #include <sys/types.h>
39 #include <sys/sysmacros.h>
40 #include <sys/systm.h>
41 #include <sys/errno.h>
42 #include <sys/fcntl.h>
43 #include <sys/pathname.h>
44 #include <sys/var.h>
45 #include <sys/vfs.h>
46 #include <sys/vnode.h>
47 #include <sys/file.h>
48 #include <sys/mode.h>
49 #include <sys/proc.h>
50 #include <sys/uio.h>
51 #include <sys/filio.h>
52 #include <sys/fcntl.h>
53 #include <sys/debug.h>
54 #include <c2/audit.h>
55 
56 /*
57  * nmflag has the following values
58  *
59  * 1 - Always do lookup.  i.e. chown, lchown.
60  * 2 - Name is optional i.e. fchownat
61  * 0 - Don't lookup name, vp is in file_p. i.e. fchown
62  *
63  */
64 int
65 cfchownat(int fd, char *name, int nmflag, uid_t uid, gid_t gid, int flags)
66 {
67 	vnode_t		*startvp, *vp;
68 	file_t 		*filefp;
69 	struct vattr 	vattr;
70 	int 		error = 0;
71 	char 		startchar;
72 	struct zone	*zone = crgetzone(CRED());
73 
74 	if (uid != (uid_t)-1 && !VALID_UID(uid, zone) ||
75 	    gid != (gid_t)-1 && !VALID_GID(gid, zone)) {
76 		return (set_errno(EINVAL));
77 	}
78 	vattr.va_uid = uid;
79 	vattr.va_gid = gid;
80 	vattr.va_mask = 0;
81 	if (vattr.va_uid != -1)
82 		vattr.va_mask |= AT_UID;
83 	if (vattr.va_gid != -1)
84 		vattr.va_mask |= AT_GID;
85 
86 
87 	if (fd == AT_FDCWD && name == NULL)
88 		return (set_errno(EFAULT));
89 
90 	if (nmflag == 1 || (nmflag == 2 && name != NULL)) {
91 		if (copyin(name, &startchar, sizeof (char)))
92 			return (set_errno(EFAULT));
93 	} else
94 		startchar = '\0';
95 
96 
97 	if (fd == AT_FDCWD)
98 		startvp = NULL;
99 	else {
100 		/*
101 		 * only get fd if not doing absolute lookup
102 		 */
103 		if (startchar != '/' || nmflag == 0) {
104 			if ((filefp = getf(fd)) == NULL) {
105 				return (set_errno(EBADF));
106 			}
107 			startvp = filefp->f_vnode;
108 			VN_HOLD(startvp);
109 			releasef(fd);
110 		} else {
111 			startvp = NULL;
112 		}
113 	}
114 
115 	if ((nmflag == 2) && audit_active)
116 		audit_setfsat_path(1);
117 
118 	/*
119 	 * Do lookups for chown, lchown and fchownat when name not NULL
120 	 */
121 	if ((nmflag == 2 && name != NULL) || nmflag == 1) {
122 		if (error = lookupnameat(name, UIO_USERSPACE,
123 		    (flags == AT_SYMLINK_NOFOLLOW) ?
124 		    NO_FOLLOW : FOLLOW,
125 		    NULLVPP, &vp, startvp)) {
126 			if (startvp != NULL)
127 				VN_RELE(startvp);
128 			return (set_errno(error));
129 		}
130 	} else {
131 		vp = startvp;
132 		ASSERT(vp);
133 		VN_HOLD(vp);
134 	}
135 
136 	if (vn_is_readonly(vp)) {
137 		error = EROFS;
138 	} else {
139 		error = VOP_SETATTR(vp, &vattr, 0, CRED(), NULL);
140 	}
141 
142 	if (startvp != NULL)
143 		VN_RELE(startvp);
144 	if (vp != NULL)
145 		VN_RELE(vp);
146 
147 	if (error != 0)
148 		return (set_errno(error));
149 	else
150 		return (error);
151 }
152 /*
153  * Change ownership of file given file name.
154  */
155 int
156 chown(char *fname, uid_t uid, gid_t gid)
157 {
158 	return (cfchownat(AT_FDCWD, fname, 1, uid, gid, 0));
159 }
160 
161 int
162 lchown(char *fname, uid_t uid, gid_t gid)
163 {
164 	return (cfchownat(AT_FDCWD, fname, 1, uid, gid, AT_SYMLINK_NOFOLLOW));
165 }
166 
167 /*
168  * Change ownership of file given file descriptor.
169  */
170 int
171 fchown(int fd, uid_t uid, uid_t gid)
172 {
173 	return (cfchownat(fd, NULL, 0, uid, gid, 0));
174 }
175 
176 int
177 fchownat(int fd, char *name, uid_t uid, gid_t gid, int flags)
178 {
179 	return (cfchownat(fd, name, 2, uid, gid, flags));
180 
181 }
182