xref: /titanic_50/usr/src/uts/common/syscall/chown.c (revision c5fb5d329e745a7b8cb9ff07eebb42948af7bc4e)
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 /*
23  * Copyright 2010 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 #include <sys/param.h>
36 #include <sys/isa_defs.h>
37 #include <sys/types.h>
38 #include <sys/sysmacros.h>
39 #include <sys/systm.h>
40 #include <sys/errno.h>
41 #include <sys/fcntl.h>
42 #include <sys/pathname.h>
43 #include <sys/var.h>
44 #include <sys/vfs.h>
45 #include <sys/vnode.h>
46 #include <sys/file.h>
47 #include <sys/mode.h>
48 #include <sys/proc.h>
49 #include <sys/uio.h>
50 #include <sys/filio.h>
51 #include <sys/fcntl.h>
52 #include <sys/debug.h>
53 #include <c2/audit.h>
54 
55 /*
56  * Change ownership of file.
57  */
58 int
59 fchownat(int fd, char *name, uid_t uid, gid_t gid, int flags)
60 {
61 	vnode_t		*startvp, *vp;
62 	file_t 		*filefp;
63 	struct vattr 	vattr;
64 	int 		error = 0;
65 	char 		startchar;
66 	struct zone	*zone = crgetzone(CRED());
67 
68 	if (uid != (uid_t)-1 && !VALID_UID(uid, zone) ||
69 	    gid != (gid_t)-1 && !VALID_GID(gid, zone)) {
70 		return (set_errno(EINVAL));
71 	}
72 	vattr.va_uid = uid;
73 	vattr.va_gid = gid;
74 	vattr.va_mask = 0;
75 	if (vattr.va_uid != -1)
76 		vattr.va_mask |= AT_UID;
77 	if (vattr.va_gid != -1)
78 		vattr.va_mask |= AT_GID;
79 
80 
81 	if (fd == AT_FDCWD && name == NULL)
82 		return (set_errno(EFAULT));
83 
84 	if (name != NULL) {
85 		if (copyin(name, &startchar, sizeof (char)))
86 			return (set_errno(EFAULT));
87 	} else {
88 		startchar = '\0';
89 	}
90 
91 
92 	if (fd == AT_FDCWD)
93 		startvp = NULL;
94 	else {
95 		/*
96 		 * only get fd if not doing absolute lookup
97 		 */
98 		if (startchar != '/') {
99 			if ((filefp = getf(fd)) == NULL)
100 				return (set_errno(EBADF));
101 			startvp = filefp->f_vnode;
102 			VN_HOLD(startvp);
103 			releasef(fd);
104 		} else {
105 			startvp = NULL;
106 		}
107 	}
108 
109 	if (AU_AUDITING())
110 		audit_setfsat_path(1);
111 
112 	/*
113 	 * Do lookup for fchownat when name not NULL
114 	 */
115 	if (name != NULL) {
116 		if (error = lookupnameat(name, UIO_USERSPACE,
117 		    (flags == AT_SYMLINK_NOFOLLOW) ?
118 		    NO_FOLLOW : FOLLOW,
119 		    NULLVPP, &vp, startvp)) {
120 			if (startvp != NULL)
121 				VN_RELE(startvp);
122 			return (set_errno(error));
123 		}
124 	} else {
125 		vp = startvp;
126 		ASSERT(vp);
127 		VN_HOLD(vp);
128 	}
129 
130 	if (vn_is_readonly(vp)) {
131 		error = EROFS;
132 	} else {
133 		error = VOP_SETATTR(vp, &vattr, 0, CRED(), NULL);
134 	}
135 
136 	if (startvp != NULL)
137 		VN_RELE(startvp);
138 	if (vp != NULL)
139 		VN_RELE(vp);
140 
141 	if (error != 0)
142 		return (set_errno(error));
143 	else
144 		return (error);
145 }
146 
147 int
148 chown(char *fname, uid_t uid, gid_t gid)
149 {
150 	return (fchownat(AT_FDCWD, fname, uid, gid, 0));
151 }
152 
153 int
154 lchown(char *fname, uid_t uid, gid_t gid)
155 {
156 	return (fchownat(AT_FDCWD, fname, uid, gid, AT_SYMLINK_NOFOLLOW));
157 }
158 
159 int
160 fchown(int fd, uid_t uid, uid_t gid)
161 {
162 	return (fchownat(fd, NULL, uid, gid, 0));
163 }
164