source: rtems/cpukit/libcsupport/src/sup_fs_check_permissions.c @ c499856

4.115
Last change on this file since c499856 was c499856, checked in by Chris Johns <chrisj@…>, on 03/20/14 at 21:10:47

Change all references of rtems.com to rtems.org.

  • Property mode set to 100644
File size: 2.3 KB
Line 
1/**
2 *  @file
3 *
4 *  @brief RTEMS File System Permissions Check Support
5 *  @ingroup LibIOInternal
6 */
7
8/*
9 * Copyright (c) 2012 embedded brains GmbH.  All rights reserved.
10 *
11 *  embedded brains GmbH
12 *  Obere Lagerstr. 30
13 *  82178 Puchheim
14 *  Germany
15 *  <rtems@embedded-brains.de>
16 *
17 * The license and distribution terms for this file may be
18 * found in the file LICENSE in this distribution or at
19 * http://www.rtems.org/license/LICENSE.
20 */
21
22#if HAVE_CONFIG_H
23  #include "config.h"
24#endif
25
26#include <sys/stat.h>
27
28#include <rtems/libio_.h>
29
30#define RTEMS_FS_USR_SHIFT 6
31#define RTEMS_FS_GRP_SHIFT 3
32#define RTEMS_FS_OTH_SHIFT 0
33
34RTEMS_STATIC_ASSERT(
35  (RTEMS_FS_PERMS_READ << RTEMS_FS_USR_SHIFT) == S_IRUSR,
36  S_IRUSR
37);
38RTEMS_STATIC_ASSERT(
39  (RTEMS_FS_PERMS_READ << RTEMS_FS_GRP_SHIFT) == S_IRGRP,
40  S_IRGRP
41);
42RTEMS_STATIC_ASSERT(
43  (RTEMS_FS_PERMS_READ << RTEMS_FS_OTH_SHIFT) == S_IROTH,
44  S_IROTH
45);
46
47RTEMS_STATIC_ASSERT(
48  (RTEMS_FS_PERMS_WRITE << RTEMS_FS_USR_SHIFT) == S_IWUSR,
49  S_IWUSR
50);
51RTEMS_STATIC_ASSERT(
52  (RTEMS_FS_PERMS_WRITE << RTEMS_FS_GRP_SHIFT) == S_IWGRP,
53  S_IWGRP
54);
55RTEMS_STATIC_ASSERT(
56  (RTEMS_FS_PERMS_WRITE << RTEMS_FS_OTH_SHIFT) == S_IWOTH,
57  S_IWOTH
58);
59
60RTEMS_STATIC_ASSERT(
61  (RTEMS_FS_PERMS_EXEC << RTEMS_FS_USR_SHIFT) == S_IXUSR,
62  S_IXUSR
63);
64RTEMS_STATIC_ASSERT(
65  (RTEMS_FS_PERMS_EXEC << RTEMS_FS_GRP_SHIFT) == S_IXGRP,
66  S_IXGRP
67);
68RTEMS_STATIC_ASSERT(
69  (RTEMS_FS_PERMS_EXEC << RTEMS_FS_OTH_SHIFT) == S_IXOTH,
70  S_IXOTH
71);
72
73bool rtems_filesystem_check_access(
74  int eval_flags,
75  mode_t node_mode,
76  uid_t node_uid,
77  gid_t node_gid
78)
79{
80  mode_t perm_flags = eval_flags & RTEMS_FS_PERMS_RWX;
81  uid_t task_uid = geteuid();
82
83  if (task_uid == 0 || task_uid == node_uid) {
84    perm_flags <<= RTEMS_FS_USR_SHIFT;
85  } else {
86    gid_t task_gid = getegid();
87
88    if (task_gid == 0 || task_gid == node_gid) {
89      perm_flags <<= RTEMS_FS_GRP_SHIFT;
90    } else {
91      perm_flags <<= RTEMS_FS_OTH_SHIFT;
92    }
93  }
94
95  return (perm_flags & node_mode) == perm_flags;
96}
97
98bool rtems_filesystem_eval_path_check_access(
99  rtems_filesystem_eval_path_context_t *ctx,
100  int eval_flags,
101  mode_t node_mode,
102  uid_t node_uid,
103  gid_t node_gid
104)
105{
106  bool access_ok = rtems_filesystem_check_access(
107    eval_flags,
108    node_mode,
109    node_uid,
110    node_gid
111  );
112
113  if (!access_ok) {
114    rtems_filesystem_eval_path_error(ctx, EACCES);
115  }
116
117  return access_ok;
118}
Note: See TracBrowser for help on using the repository browser.