]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tools/regression/priv/test_utimes.c
This commit was generated by cvs2svn to compensate for changes in r168305,
[FreeBSD/FreeBSD.git] / tools / regression / priv / test_utimes.c
1 /*-
2  * Copyright (c) 2006 nCircle Network Security, Inc.
3  * All rights reserved.
4  *
5  * This software was developed by Robert N. M. Watson for the TrustedBSD
6  * Project under contract to nCircle Network Security, Inc.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
16  *
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.
28  *
29  * $FreeBSD$
30  */
31
32 /*
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.
35  *
36  * If times is non-NULL, ...  The caller must be the owner of the file or be
37  * the super-user.
38  *
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.
42  */
43
44 #include <sys/types.h>
45 #include <sys/stat.h>
46
47 #include <err.h>
48 #include <errno.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <unistd.h>
53
54 #include "main.h"
55
56 static int
57 try_utimes(const char *path, mode_t mode, uid_t uid,
58     struct timeval *timestamp, int expected)
59 {
60         int error;
61
62         if (chmod(path, mode) < 0) {
63                 warn("try_utimes(%s, %d, %d, 0x%08x): chmod", path, mode, uid,
64                     (u_int)timestamp);
65                 (void)unlink(path);
66                 exit(-1);
67         }
68
69         if (seteuid(uid) < 0) {
70                 warn("try_utimes(%s, %d, %d, 0x%08x): seteuid(%d)", path,
71                     mode, uid, (u_int)timestamp, uid);
72                 (void)unlink(path);
73                 exit(-1);
74         }
75
76         error = utimes(path, timestamp);
77
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);
81                 (void)unlink(path);
82                 exit(-1);
83         }
84
85         if (expected == 0) {
86                 if (error != 0) {
87                         (void)unlink(path);
88                         errx(-1, "try_utimes(%s, 0%o, %d, 0x%08x) failed %d",
89                             path, mode, uid, (u_int)timestamp, errno);
90                 }
91                 return (0);
92         }
93
94         if (expected == errno)
95                 return (0);
96
97         (void)unlink(path);
98         errx(-1, "try_utimes(%s, 0%o, %d, 0x%08x) wrong err %d", path, mode,
99             uid, (u_int)timestamp, errno);
100 }
101
102 void
103 test_utimes(void)
104 {
105         char path[128] = "/tmp/utimes.XXXXXXXXX";
106         struct timeval timestamp[2];
107         int fd;
108
109         if (getuid() != 0)
110                 errx(-1, "must be run as root");
111
112         fd = mkstemp(path);
113         if (fd == -1)
114                 err(-1, "mkstemp");
115
116         if (chown(path, UID_OWNER, -1) < 0) {
117                 warn("chown(%s, %d)", path, UID_OWNER);
118                 (void)unlink(path);
119                 return;
120         }
121
122         bzero(timestamp, sizeof(timestamp));
123
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);
128
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);
132
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);
137
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);
142
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);
146
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);
151
152         (void)unlink(path);
153 }