xref: /titanic_50/usr/src/uts/common/syscall/chown.c (revision 2df1fe9ca32bb227b9158c67f5c00b54c20b10fd)
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 2007 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 
73 	if (uid != (uid_t)-1 && !VALID_UID(uid) ||
74 	    gid != (gid_t)-1 && !VALID_GID(gid)) {
75 		return (set_errno(EINVAL));
76 	}
77 	vattr.va_uid = uid;
78 	vattr.va_gid = gid;
79 	vattr.va_mask = 0;
80 	if (vattr.va_uid != -1)
81 		vattr.va_mask |= AT_UID;
82 	if (vattr.va_gid != -1)
83 		vattr.va_mask |= AT_GID;
84 
85 
86 	if (fd == AT_FDCWD && name == NULL)
87 		return (set_errno(EFAULT));
88 
89 	if (nmflag == 1 || (nmflag == 2 && name != NULL)) {
90 		if (copyin(name, &startchar, sizeof (char)))
91 			return (set_errno(EFAULT));
92 	} else
93 		startchar = '\0';
94 
95 
96 	if (fd == AT_FDCWD)
97 		startvp = NULL;
98 	else {
99 		/*
100 		 * only get fd if not doing absolute lookup
101 		 */
102 		if (startchar != '/' || nmflag == 0) {
103 			if ((filefp = getf(fd)) == NULL) {
104 				return (set_errno(EBADF));
105 			}
106 			startvp = filefp->f_vnode;
107 			VN_HOLD(startvp);
108 			releasef(fd);
109 		} else {
110 			startvp = NULL;
111 		}
112 	}
113 
114 #if C2_AUDIT
115 	if ((nmflag == 2) && audit_active)
116 		audit_setfsat_path(1);
117 #endif /* C2_AUDIT */
118 
119 	/*
120 	 * Do lookups for chown, lchown and fchownat when name not NULL
121 	 */
122 	if ((nmflag == 2 && name != NULL) || nmflag == 1) {
123 		if (error = lookupnameat(name, UIO_USERSPACE,
124 		    (flags == AT_SYMLINK_NOFOLLOW) ?
125 		    NO_FOLLOW : FOLLOW,
126 		    NULLVPP, &vp, startvp)) {
127 			if (startvp != NULL)
128 				VN_RELE(startvp);
129 			return (set_errno(error));
130 		}
131 	} else {
132 		vp = startvp;
133 		ASSERT(vp);
134 		VN_HOLD(vp);
135 	}
136 
137 	if (vn_is_readonly(vp)) {
138 		error = EROFS;
139 	} else {
140 		error = VOP_SETATTR(vp, &vattr, 0, CRED(), NULL);
141 	}
142 
143 	if (startvp != NULL)
144 		VN_RELE(startvp);
145 	if (vp != NULL)
146 		VN_RELE(vp);
147 
148 	if (error != 0)
149 		return (set_errno(error));
150 	else
151 		return (error);
152 }
153 /*
154  * Change ownership of file given file name.
155  */
156 int
157 chown(char *fname, uid_t uid, gid_t gid)
158 {
159 	return (cfchownat(AT_FDCWD, fname, 1, uid, gid, 0));
160 }
161 
162 int
163 lchown(char *fname, uid_t uid, gid_t gid)
164 {
165 	return (cfchownat(AT_FDCWD, fname, 1, uid, gid, AT_SYMLINK_NOFOLLOW));
166 }
167 
168 /*
169  * Change ownership of file given file descriptor.
170  */
171 int
172 fchown(int fd, uid_t uid, uid_t gid)
173 {
174 	return (cfchownat(fd, NULL, 0, uid, gid, 0));
175 }
176 
177 int
178 fchownat(int fd, char *name, uid_t uid, gid_t gid, int flags)
179 {
180 	return (cfchownat(fd, name, 2, uid, gid, flags));
181 
182 }
183