1 /* Test for chmod functions.
2 Copyright (C) 2000-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
31 #define OUT_OF_MEMORY \
33 puts ("cannot allocate memory"); \
39 do_test (int argc
, char *argv
[])
46 char *testfile
= NULL
;
56 error (EXIT_FAILURE
, 0, "no parameters");
58 /* This is where we will create the test files. */
60 buflen
= strlen (builddir
) + 50;
62 startdir
= getcwd (NULL
, 0);
65 printf ("cannot get current directory: %m\n");
69 /* A buffer large enough for everything we need. */
70 buf
= (char *) alloca (buflen
);
72 /* Create the directory name. */
73 snprintf (buf
, buflen
, "%s/chmoddirXXXXXX", builddir
);
75 if (mkdtemp (buf
) == NULL
)
77 printf ("cannot create test directory: %m\n");
81 if (chmod ("", 0600) == 0)
83 puts ("chmod(\"\", 0600 didn't fail");
86 else if (errno
!= ENOENT
)
88 puts ("chmod(\"\",0600) does not set errno to ENOENT");
92 /* Create a duplicate. */
93 testdir
= strdup (buf
);
97 if (stat64 (testdir
, &st1
) != 0)
99 printf ("cannot stat test directory: %m\n");
102 if (!S_ISDIR (st1
.st_mode
))
104 printf ("file not created as directory: %m\n");
108 /* We have to wait for a second to make sure the ctime changes. */
111 /* Remove all access rights from the directory. */
112 if (chmod (testdir
, 0) != 0)
114 printf ("cannot change mode of test directory: %m\n");
119 if (stat64 (testdir
, &st2
) != 0)
121 printf ("cannot stat test directory: %m\n");
126 /* Compare result. */
127 if ((st2
.st_mode
& ALLPERMS
) != 0)
129 printf ("chmod(...,0) on directory left bits nonzero: %o\n",
130 st2
.st_mode
& ALLPERMS
);
133 if (st1
.st_ctime
>= st2
.st_ctime
)
135 puts ("chmod(...,0) did not set ctime correctly");
139 /* Name of a file in the directory. */
140 snprintf (buf
, buflen
, "%s/file", testdir
);
141 testfile
= strdup (buf
);
142 if (testfile
== NULL
)
145 fd
= creat (testfile
, 0);
150 puts ("managed to create test file in protected directory");
155 else if (errno
!= EACCES
)
157 puts ("creat didn't generate correct errno value");
161 /* With this mode it still shouldn't be possible to create a file. */
162 if (chmod (testdir
, 0600) != 0)
164 printf ("cannot change mode of test directory to 0600: %m\n");
169 fd
= creat (testfile
, 0);
174 puts ("managed to create test file in no-x protected directory");
179 else if (errno
!= EACCES
)
181 puts ("creat didn't generate correct errno value");
185 /* Change the directory mode back to allow creating a file. This
187 dir
= opendir (testdir
);
190 if (fchmod (dirfd (dir
), 0700) != 0)
192 printf ("cannot change mode of test directory to 0700: %m\n");
202 printf ("cannot open directory: %m\n");
205 if (chmod (testdir
, 0700) != 0)
207 printf ("cannot change mode of test directory to 0700: %m\n");
212 fd
= creat (testfile
, 0);
215 puts ("still didn't manage to create test file in protected directory");
219 if (fstat64 (fd
, &st1
) != 0)
221 printf ("cannot stat new file: %m\n");
224 else if ((st1
.st_mode
& ALLPERMS
) != 0)
226 puts ("file not created with access mode 0");
231 snprintf (buf
, buflen
, "%s/..", testdir
);
233 /* We are now in the directory above the one we create the test
237 snprintf (buf
, buflen
, "./%s/../%s/file",
238 basename (testdir
), basename (testdir
));
239 if (chmod (buf
, 0600) != 0)
241 printf ("cannot change mode of file to 0600: %m\n");
245 snprintf (buf
, buflen
, "./%s//file", basename (testdir
));
246 if (stat64 (buf
, &st2
) != 0)
248 printf ("cannot stat new file: %m\n");
251 else if ((st2
.st_mode
& ALLPERMS
) != 0600)
253 puts ("file mode not changed to 0600");
256 else if (st1
.st_ctime
>= st2
.st_ctime
)
258 puts ("chmod(\".../file\",0600) did not set ctime correctly");
262 if (chmod (buf
, 0777 | S_ISUID
| S_ISGID
) != 0)
264 printf ("cannot change mode of file to %o: %m\n",
265 0777 | S_ISUID
| S_ISGID
);
268 if (stat64 (buf
, &st2
) != 0)
270 printf ("cannot stat test file: %m\n");
273 else if ((st2
.st_mode
& ALLPERMS
) != (0777 | S_ISUID
| S_ISGID
))
275 puts ("file mode not changed to 0777 | S_ISUID | S_ISGID");
279 if (chmod (basename (testdir
), 0777 | S_ISUID
| S_ISGID
| S_ISVTX
) != 0)
281 printf ("cannot change mode of test directory to %o: %m\n",
282 0777 | S_ISUID
| S_ISGID
| S_ISVTX
);
285 if (stat64 (basename (testdir
), &st2
) != 0)
287 printf ("cannot stat test directory: %m\n");
290 else if ((st2
.st_mode
& ALLPERMS
) != (0777 | S_ISUID
| S_ISGID
| S_ISVTX
))
292 puts ("directory mode not changed to 0777 | S_ISUID | S_ISGID | S_ISGID");
296 snprintf (buf
, buflen
, "./%s/no-such-file", basename (testdir
));
297 if (chmod (buf
, 0600) != -1)
299 puts ("chmod(\".../no-such-file\",0600) did not fail");
302 else if (errno
!= ENOENT
)
304 puts ("chmod(\".../no-such-file\",0600) does not set errno to ENOENT");
308 snprintf (buf
, buflen
, "%s/", basename (testdir
));
309 if (chmod (basename (testdir
), 0677) != 0)
311 printf ("cannot change mode of test directory to 0677: %m\n");
316 snprintf (buf
, buflen
, "./%s/file", basename (testdir
));
317 if (chmod (buf
, 0600) == 0)
321 puts ("chmod(\".../file\") with no-exec directory succeeded");
325 else if (errno
!= EACCES
)
327 puts ("chmod(\".../file\") with no-exec directory didn't set EACCES");
332 if (chmod (basename (testdir
), 0777) != 0)
334 printf ("cannot change mode of test directory to 0777: %m\n");
339 snprintf (buf
, buflen
, "%s/file/cannot-be", basename (testdir
));
340 if (chmod (buf
, 0600) == 0)
342 puts ("chmod(\".../file/cannot-be\",0600) did not fail");
345 else if (errno
!= ENOTDIR
)
347 puts ("chmod(\".../file/cannot-be\",0600) does not set errno to ENOTDIR");
354 /* Remove all the files. */
355 chmod (testdir
, 0700);
356 if (testfile
!= NULL
)
358 chmod (testfile
, 0700);
363 /* Free the resources. */
371 #include "../test-skeleton.c"