1 | /* provide consistent interface to chown for systems that don't interpret
|
---|
2 | an ID of -1 as meaning `don't change the corresponding ID'.
|
---|
3 |
|
---|
4 | Copyright (C) 1997, 2004, 2005, 2006 Free Software Foundation, Inc.
|
---|
5 |
|
---|
6 | This program is free software; you can redistribute it and/or modify
|
---|
7 | it under the terms of the GNU General Public License as published by
|
---|
8 | the Free Software Foundation; either version 2, or (at your option)
|
---|
9 | any later version.
|
---|
10 |
|
---|
11 | This program is distributed in the hope that it will be useful,
|
---|
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | GNU General Public License for more details.
|
---|
15 |
|
---|
16 | You should have received a copy of the GNU General Public License
|
---|
17 | along with this program; if not, write to the Free Software Foundation,
|
---|
18 | Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
|
---|
19 |
|
---|
20 | /* written by Jim Meyering */
|
---|
21 |
|
---|
22 | #include <config.h>
|
---|
23 |
|
---|
24 | /* Disable the definition of chown to rpl_chown (from config.h) in this
|
---|
25 | file. Otherwise, we'd get conflicting prototypes for rpl_chown on
|
---|
26 | most systems. */
|
---|
27 | #undef chown
|
---|
28 |
|
---|
29 | #include <stdbool.h>
|
---|
30 | #include <sys/types.h>
|
---|
31 | #include <sys/stat.h>
|
---|
32 | #include <unistd.h>
|
---|
33 | #include <fcntl.h>
|
---|
34 | #include <errno.h>
|
---|
35 |
|
---|
36 | /* Provide a more-closely POSIX-conforming version of chown on
|
---|
37 | systems with one or both of the following problems:
|
---|
38 | - chown doesn't treat an ID of -1 as meaning
|
---|
39 | `don't change the corresponding ID'.
|
---|
40 | - chown doesn't dereference symlinks. */
|
---|
41 |
|
---|
42 | int
|
---|
43 | rpl_chown (const char *file, uid_t uid, gid_t gid)
|
---|
44 | {
|
---|
45 | #if CHOWN_FAILS_TO_HONOR_ID_OF_NEGATIVE_ONE
|
---|
46 | if (gid == (gid_t) -1 || uid == (uid_t) -1)
|
---|
47 | {
|
---|
48 | struct stat file_stats;
|
---|
49 |
|
---|
50 | /* Stat file to get id(s) that should remain unchanged. */
|
---|
51 | if (stat (file, &file_stats))
|
---|
52 | return -1;
|
---|
53 |
|
---|
54 | if (gid == (gid_t) -1)
|
---|
55 | gid = file_stats.st_gid;
|
---|
56 |
|
---|
57 | if (uid == (uid_t) -1)
|
---|
58 | uid = file_stats.st_uid;
|
---|
59 | }
|
---|
60 | #endif
|
---|
61 |
|
---|
62 | #if CHOWN_MODIFIES_SYMLINK
|
---|
63 | {
|
---|
64 | /* Handle the case in which the system-supplied chown function
|
---|
65 | does *not* follow symlinks. Instead, it changes permissions
|
---|
66 | on the symlink itself. To work around that, we open the
|
---|
67 | file (but this can fail due to lack of read or write permission) and
|
---|
68 | use fchown on the resulting descriptor. */
|
---|
69 | int open_flags = O_NONBLOCK | O_NOCTTY;
|
---|
70 | int fd = open (file, O_RDONLY | open_flags);
|
---|
71 | if (0 <= fd
|
---|
72 | || (errno == EACCES
|
---|
73 | && 0 <= (fd = open (file, O_WRONLY | open_flags))))
|
---|
74 | {
|
---|
75 | int result = fchown (fd, uid, gid);
|
---|
76 | int saved_errno = errno;
|
---|
77 |
|
---|
78 | /* POSIX says fchown can fail with errno == EINVAL on sockets,
|
---|
79 | so fall back on chown in that case. */
|
---|
80 | struct stat sb;
|
---|
81 | bool fchown_socket_failure =
|
---|
82 | (result != 0 && saved_errno == EINVAL
|
---|
83 | && fstat (fd, &sb) == 0 && S_ISFIFO (sb.st_mode));
|
---|
84 |
|
---|
85 | close (fd);
|
---|
86 |
|
---|
87 | if (! fchown_socket_failure)
|
---|
88 | {
|
---|
89 | errno = saved_errno;
|
---|
90 | return result;
|
---|
91 | }
|
---|
92 | }
|
---|
93 | else if (errno != EACCES)
|
---|
94 | return -1;
|
---|
95 | }
|
---|
96 | #endif
|
---|
97 |
|
---|
98 | return chown (file, uid, gid);
|
---|
99 | }
|
---|