1 | /* SPDX-License-Identifier: BSD-2-Clause */ |
---|
2 | |
---|
3 | /** |
---|
4 | * @file |
---|
5 | * |
---|
6 | * @brief Change Owner and Group of a File |
---|
7 | * @ingroup libcsupport |
---|
8 | */ |
---|
9 | |
---|
10 | /* |
---|
11 | * COPYRIGHT (c) 1989-1999. |
---|
12 | * On-Line Applications Research Corporation (OAR). |
---|
13 | * |
---|
14 | * Redistribution and use in source and binary forms, with or without |
---|
15 | * modification, are permitted provided that the following conditions |
---|
16 | * are met: |
---|
17 | * 1. Redistributions of source code must retain the above copyright |
---|
18 | * notice, this list of conditions and the following disclaimer. |
---|
19 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
20 | * notice, this list of conditions and the following disclaimer in the |
---|
21 | * documentation and/or other materials provided with the distribution. |
---|
22 | * |
---|
23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
27 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
28 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
29 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
32 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
33 | * POSSIBILITY OF SUCH DAMAGE. |
---|
34 | */ |
---|
35 | |
---|
36 | #ifdef HAVE_CONFIG_H |
---|
37 | #include "config.h" |
---|
38 | #endif |
---|
39 | |
---|
40 | #include <string.h> |
---|
41 | #include <unistd.h> |
---|
42 | |
---|
43 | #include <rtems/libio_.h> |
---|
44 | |
---|
45 | int rtems_filesystem_chown( |
---|
46 | const rtems_filesystem_location_info_t *loc, |
---|
47 | uid_t owner, |
---|
48 | gid_t group |
---|
49 | ) |
---|
50 | { |
---|
51 | const rtems_filesystem_mount_table_entry_t *mt_entry = loc->mt_entry; |
---|
52 | int rv; |
---|
53 | |
---|
54 | if ( mt_entry->writeable || rtems_filesystem_location_is_null( loc ) ) { |
---|
55 | struct stat st; |
---|
56 | |
---|
57 | memset( &st, 0, sizeof(st) ); |
---|
58 | |
---|
59 | rv = (*loc->handlers->fstat_h)( loc, &st ); |
---|
60 | if ( rv == 0 ) { |
---|
61 | uid_t uid = geteuid(); |
---|
62 | |
---|
63 | if ( uid == 0 || st.st_uid == uid ) { |
---|
64 | rv = (*mt_entry->ops->chown_h)( loc, owner, group ); |
---|
65 | } else { |
---|
66 | errno = EPERM; |
---|
67 | rv = -1; |
---|
68 | } |
---|
69 | } |
---|
70 | } else { |
---|
71 | errno = EROFS; |
---|
72 | rv = -1; |
---|
73 | } |
---|
74 | |
---|
75 | return rv; |
---|
76 | } |
---|
77 | |
---|
78 | /** |
---|
79 | * POSIX 1003.1b 5.6.5 - Change Owner and Group of a File |
---|
80 | */ |
---|
81 | int fchown( int fd, uid_t owner, gid_t group ) |
---|
82 | { |
---|
83 | int rv; |
---|
84 | rtems_libio_t *iop; |
---|
85 | |
---|
86 | LIBIO_GET_IOP( fd, iop ); |
---|
87 | |
---|
88 | rtems_filesystem_instance_lock( &iop->pathinfo ); |
---|
89 | |
---|
90 | rv = rtems_filesystem_chown( &iop->pathinfo, owner, group ); |
---|
91 | |
---|
92 | rtems_filesystem_instance_unlock( &iop->pathinfo ); |
---|
93 | |
---|
94 | rtems_libio_iop_drop( iop ); |
---|
95 | |
---|
96 | return rv; |
---|
97 | } |
---|