Correct BSD License clause numbering from 1-2-4 to 1-2-3.
[dragonfly.git] / lib / libc / sys / chmod.2
1 .\" Copyright (c) 1980, 1991, 1993
2 .\"     The Regents of the University of California.  All rights reserved.
3 .\"
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
6 .\" are met:
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\"    notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\"    notice, this list of conditions and the following disclaimer in the
11 .\"    documentation and/or other materials provided with the distribution.
12 .\" 3. Neither the name of the University nor the names of its contributors
13 .\"    may be used to endorse or promote products derived from this software
14 .\"    without specific prior written permission.
15 .\"
16 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 .\" SUCH DAMAGE.
27 .\"
28 .\"     @(#)chmod.2     8.1 (Berkeley) 6/4/93
29 .\" $FreeBSD: src/lib/libc/sys/chmod.2,v 1.16.2.7 2001/12/14 18:34:00 ru Exp $
30 .\"
31 .Dd August 9, 2009
32 .Dt CHMOD 2
33 .Os
34 .Sh NAME
35 .Nm chmod ,
36 .Nm fchmod ,
37 .Nm lchmod ,
38 .Nm fchmodat
39 .Nd change mode of file
40 .Sh LIBRARY
41 .Lb libc
42 .Sh SYNOPSIS
43 .In sys/stat.h
44 .Ft int
45 .Fn chmod "const char *path" "mode_t mode"
46 .Ft int
47 .Fn fchmod "int fd" "mode_t mode"
48 .Ft int
49 .Fn lchmod "const char *path" "mode_t mode"
50 .Ft int
51 .Fn fchmodat "int dirfd" "const char *path" "mode_t mode" "int flags"
52 .Sh DESCRIPTION
53 The file permission bits of the file named specified by
54 .Fa path
55 or referenced by the file descriptor
56 .Fa fd
57 are changed to
58 .Fa mode .
59 The
60 .Fn chmod
61 function verifies that the process owner (user) either owns
62 the file specified by
63 .Fa path
64 (or
65 .Fa fd ) ,
66 or
67 is the super-user.
68 The
69 .Fn chmod
70 function follows symbolic links to operate on the target of the link
71 rather than the link itself.
72 .Pp
73 The
74 .Fa lchmod
75 function is similar to
76 .Fn chmod
77 but does not follow symbolic links.
78 .Pp
79 The
80 .Fn fchmodat
81 function is equivalent to the
82 .Fn chmod
83 or
84 .Fn lchmod
85 functions except in the case where the
86 .Fa path
87 specifies a relative path.
88 In this case the file to be opened is determined relative to the directory
89 associated with the file descriptor
90 .Fa dirfd
91 instead of the current working directory.
92 If
93 .Fn fchmodat
94 is passed the special value
95 .Dv AT_FDCWD
96 in the
97 .Fa dirfd
98 parameter, the current working directory is used
99 and the behavior is identical to a call to
100 .Fn chmod
101 or
102 .Fn lchmod .
103 .Pp
104 A mode is created from
105 .Em or'd
106 permission bit masks
107 defined in
108 .In sys/stat.h :
109 .Pp
110 .Bd -literal -offset indent -compact
111 #define S_IRWXU 0000700    /* RWX mask for owner */
112 #define S_IRUSR 0000400    /* R for owner */
113 #define S_IWUSR 0000200    /* W for owner */
114 #define S_IXUSR 0000100    /* X for owner */
115
116 #define S_IRWXG 0000070    /* RWX mask for group */
117 #define S_IRGRP 0000040    /* R for group */
118 #define S_IWGRP 0000020    /* W for group */
119 #define S_IXGRP 0000010    /* X for group */
120
121 #define S_IRWXO 0000007    /* RWX mask for other */
122 #define S_IROTH 0000004    /* R for other */
123 #define S_IWOTH 0000002    /* W for other */
124 #define S_IXOTH 0000001    /* X for other */
125
126 #define S_ISUID 0004000    /* set user id on execution */
127 #define S_ISGID 0002000    /* set group id on execution */
128 #define S_ISVTX 0001000    /* sticky bit */
129 #ifndef _POSIX_SOURCE
130 #define S_ISTXT 0001000
131 #endif
132 .Ed
133 .Pp
134 The
135 .Dx
136 VM system totally ignores the sticky bit
137 .Pq Dv ISVTX
138 for executables.
139 On UFS-based filesystems (FFS, MFS, LFS) the sticky
140 bit may only be set upon directories.
141 .Pp
142 If mode
143 .Dv ISVTX
144 (the `sticky bit') is set on a directory,
145 an unprivileged user may not delete or rename
146 files of other users in that directory.
147 The sticky bit may be
148 set by any user on a directory which the user owns or has appropriate
149 permissions.
150 For more details of the properties of the sticky bit, see
151 .Xr sticky 8 .
152 .Pp
153 If mode ISUID (set UID) is set on a directory,
154 and the MNT_SUIDDIR option was used in the mount of the filesystem,
155 then the owner of any new files and sub-directories
156 created within this directory are set
157 to be the same as the owner of that directory.
158 If this function is enabled, new directories will inherit
159 the bit from their parents.  Execute bits are removed from
160 the file, and it will not be given to root.
161 This behavior does not change the
162 requirements for the user to be allowed to write the file, but only the eventual
163 owner after it has been created.
164 Group inheritance is not affected.
165 .Pp
166 This feature is designed for use on fileservers serving PC users via
167 ftp or SAMBA.
168 It provides security holes for shell users and as
169 such should not be used on shell machines, especially on home directories.
170 This option requires the SUIDDIR
171 option in the kernel to work.
172 Only UFS filesystems support this option.
173 For more details of the suiddir mount option, see
174 .Xr mount 8 .
175 .Pp
176 Writing or changing the owner of a file
177 turns off the set-user-id and set-group-id bits
178 unless the user is the super-user.
179 This makes the system somewhat more secure
180 by protecting set-user-id (set-group-id) files
181 from remaining set-user-id (set-group-id) if they are modified,
182 at the expense of a degree of compatibility.
183 .Pp
184 The values for the
185 .Fa flags
186 are constructed by a bitwise-inclusive OR of flags from the following list,
187 defined in
188 .In fcntl.h :
189 .Bl -tag -width indent
190 .It Dv AT_SYMLINK_NOFOLLOW
191 If
192 .Fa path
193 names a symbolic link, the mode of the symbolic link is changed.
194 .El
195 .Sh RETURN VALUES
196 .Rv -std
197 .Sh ERRORS
198 .Fn Chmod ,
199 .Fn lchmod
200 and
201 .Fn fchmodat
202 will fail and the file mode will be unchanged if:
203 .Bl -tag -width Er
204 .It Bq Er ENOTDIR
205 A component of the path prefix or
206 .Fa dirfd
207 is not a directory.
208 .It Bq Er ENAMETOOLONG
209 A component of a pathname exceeded 255 characters,
210 or an entire path name exceeded 1023 characters.
211 .It Bq Er ENOENT
212 The named file does not exist.
213 .It Bq Er EACCES
214 Search permission is denied for a component of the path prefix.
215 .It Bq Er ELOOP
216 Too many symbolic links were encountered in translating the pathname.
217 .It Bq Er EPERM
218 The effective user ID does not match the owner of the file and
219 the effective user ID is not the super-user.
220 .It Bq Er EROFS
221 The named file resides on a read-only file system.
222 .It Bq Er EFAULT
223 .Fa Path
224 points outside the process's allocated address space.
225 .It Bq Er EIO
226 An I/O error occurred while reading from or writing to the file system.
227 .It Bq Er EFTYPE
228 An attempt was made to set the sticky bit upon an executable.
229 .El
230 .Pp
231 .Fn Fchmod
232 will fail if:
233 .Bl -tag -width Er
234 .It Bq Er EBADF
235 The descriptor is not valid.
236 .It Bq Er EINVAL
237 .Fa fd
238 refers to a socket, not to a file.
239 .It Bq Er EROFS
240 The file resides on a read-only file system.
241 .It Bq Er EIO
242 An I/O error occurred while reading from or writing to the file system.
243 .El
244 .Sh SEE ALSO
245 .Xr chmod 1 ,
246 .Xr chown 2 ,
247 .Xr open 2 ,
248 .Xr stat 2 ,
249 .Xr sticky 8
250 .Sh STANDARDS
251 The
252 .Fn chmod
253 function call is expected to conform to
254 .St -p1003.1-90 ,
255 except for the return of
256 .Er EFTYPE
257 and the use of
258 .Dv S_ISTXT .
259 .Sh HISTORY
260 A
261 .Fn chmod
262 function call appeared in
263 .At v7 .
264 The
265 .Fn fchmod
266 function call
267 appeared in
268 .Bx 4.2 .
269 The
270 .Fn lchmod
271 function call appeared in
272 .Fx 3.0 .
273 .Pp
274 The
275 .Fn fchmodat
276 system call appeared in
277 .Dx 2.3 .