2 * Copyright (c) 2006 nCircle Network Security, Inc.
5 * This software was developed by Robert N. M. Watson for the TrustedBSD
6 * Project under contract to nCircle Network Security, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR, NCIRCLE NETWORK SECURITY,
21 * INC., OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
23 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * If times is NULL, ... The caller must be the owner of the file, have
34 * permission to write the file, or be the super-user.
36 * If times is non-NULL, ... The caller must be the owner of the file or be
39 * To test these, create a temporary file owned by uid_owner; then run a
40 * series of tests as root, owner, and other, along with various modes, to
41 * see what is permitted, and if not, what error is returned.
44 #include <sys/types.h>
57 try_utimes(const char *path, mode_t mode, uid_t uid,
58 struct timeval *timestamp, int expected)
62 if (chmod(path, mode) < 0) {
63 warn("try_utimes(%s, %d, %d, 0x%08x): chmod", path, mode, uid,
69 if (seteuid(uid) < 0) {
70 warn("try_utimes(%s, %d, %d, 0x%08x): seteuid(%d)", path,
71 mode, uid, (u_int)timestamp, uid);
76 error = utimes(path, timestamp);
78 if (seteuid(UID_ROOT) < 0) {
79 warn("try_utimes(%s, %d, %d, 0x%08x): seteuid(UID_ROOT)",
80 path, mode, uid, (u_int)timestamp);
88 errx(-1, "try_utimes(%s, 0%o, %d, 0x%08x) failed %d",
89 path, mode, uid, (u_int)timestamp, errno);
94 if (expected == errno)
98 errx(-1, "try_utimes(%s, 0%o, %d, 0x%08x) wrong err %d", path, mode,
99 uid, (u_int)timestamp, errno);
105 char path[128] = "/tmp/utimes.XXXXXXXXX";
106 struct timeval timestamp[2];
110 errx(-1, "must be run as root");
116 if (chown(path, UID_OWNER, -1) < 0) {
117 warn("chown(%s, %d)", path, UID_OWNER);
122 bzero(timestamp, sizeof(timestamp));
124 try_utimes(path, 0444, UID_ROOT, NULL, 0);
125 try_utimes(path, 0444, UID_OWNER, NULL, 0);
126 /* Denied by permissions. */
127 try_utimes(path, 0444, UID_OTHER, NULL, EACCES);
129 try_utimes(path, 0444, UID_ROOT, timestamp, 0);
130 try_utimes(path, 0444, UID_OWNER, timestamp, 0);
131 try_utimes(path, 0444, UID_OTHER, timestamp, EPERM);
133 try_utimes(path, 0644, UID_ROOT, NULL, 0);
134 try_utimes(path, 0644, UID_OWNER, NULL, 0);
135 /* Denied by permissions. */
136 try_utimes(path, 0644, UID_OTHER, NULL, EACCES);
138 try_utimes(path, 0644, UID_ROOT, timestamp, 0);
139 try_utimes(path, 0644, UID_OWNER, timestamp, 0);
140 /* Denied as not owner. */
141 try_utimes(path, 0644, UID_OTHER, timestamp, EPERM);
143 try_utimes(path, 0666, UID_ROOT, NULL, 0);
144 try_utimes(path, 0666, UID_OWNER, NULL, 0);
145 try_utimes(path, 0666, UID_OTHER, NULL, 0);
147 try_utimes(path, 0666, UID_ROOT, timestamp, 0);
148 try_utimes(path, 0666, UID_OWNER, timestamp, 0);
149 /* Denied as not owner. */
150 try_utimes(path, 0666, UID_OTHER, timestamp, EPERM);