xref: /illumos-gate/usr/src/uts/common/syscall/chown.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 /*
31  * Portions of this source code were derived from Berkeley 4.3 BSD
32  * under license from the Regents of the University of California.
33  */
34 
35 #pragma ident	"%Z%%M%	%I%	%E% SMI"
36 
37 #include <sys/param.h>
38 #include <sys/isa_defs.h>
39 #include <sys/types.h>
40 #include <sys/sysmacros.h>
41 #include <sys/systm.h>
42 #include <sys/errno.h>
43 #include <sys/fcntl.h>
44 #include <sys/pathname.h>
45 #include <sys/var.h>
46 #include <sys/vfs.h>
47 #include <sys/vnode.h>
48 #include <sys/file.h>
49 #include <sys/mode.h>
50 #include <sys/proc.h>
51 #include <sys/uio.h>
52 #include <sys/filio.h>
53 #include <sys/fcntl.h>
54 #include <sys/debug.h>
55 #include <c2/audit.h>
56 
57 /*
58  * nmflag has the following values
59  *
60  * 1 - Always do lookup.  i.e. chown, lchown.
61  * 2 - Name is optional i.e. fchownat
62  * 0 - Don't lookup name, vp is in file_p. i.e. fchown
63  *
64  */
65 int
66 cfchownat(int fd, char *name, int nmflag, uid_t uid, gid_t gid, int flags)
67 {
68 	vnode_t		*startvp, *vp;
69 	file_t 		*filefp;
70 	struct vattr 	vattr;
71 	int 		error = 0;
72 	char 		startchar;
73 
74 	if (uid < -1 || uid > MAXUID || gid < -1 || gid > MAXUID)
75 		return (set_errno(EINVAL));
76 	vattr.va_uid = uid;
77 	vattr.va_gid = gid;
78 	vattr.va_mask = 0;
79 	if (vattr.va_uid != -1)
80 		vattr.va_mask |= AT_UID;
81 	if (vattr.va_gid != -1)
82 		vattr.va_mask |= AT_GID;
83 
84 
85 	if (fd == AT_FDCWD && name == NULL)
86 		return (set_errno(EFAULT));
87 
88 	if (nmflag == 1 || (nmflag == 2 && name != NULL)) {
89 		if (copyin(name, &startchar, sizeof (char)))
90 			return (set_errno(EFAULT));
91 	} else
92 		startchar = '\0';
93 
94 
95 	if (fd == AT_FDCWD)
96 		startvp = NULL;
97 	else {
98 		/*
99 		 * only get fd if not doing absolute lookup
100 		 */
101 		if (startchar != '/' || nmflag == 0) {
102 			if ((filefp = getf(fd)) == NULL) {
103 				return (set_errno(EBADF));
104 			}
105 			startvp = filefp->f_vnode;
106 			VN_HOLD(startvp);
107 			releasef(fd);
108 		} else {
109 			startvp = NULL;
110 		}
111 	}
112 
113 #if C2_AUDIT
114 	if ((nmflag == 2) && audit_active)
115 		audit_setfsat_path(1);
116 #endif /* C2_AUDIT */
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