File: test-access.h

package info (click to toggle)
coreutils 9.7-3
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 67,780 kB
  • sloc: ansic: 243,477; sh: 29,063; perl: 7,908; yacc: 1,858; makefile: 196; python: 47; sed: 16
file content (144 lines) | stat: -rw-r--r-- 4,203 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* Tests of access and euidaccess.
   Copyright (C) 2019-2025 Free Software Foundation, Inc.

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */

/* mingw and MSVC 9 lack geteuid, so setup a dummy value.  */
#if !HAVE_GETEUID
# define geteuid() ROOT_UID
#endif

#if defined __CYGWIN__
/* Cygwin uses the security model from Windows.  */
# include <grp.h>
# include <string.h>
# include <unistd.h>
/* Test whether the current user is in the 'Administrators' group.
   We cannot use the group name "Administrators", due to internationalization.
   Therefore test for the SID "S-1-5-32-544" (cf.
   <https://learn.microsoft.com/en-us/windows/win32/secauthz/well-known-sids>).
 */
static int
is_administrator (void)
{
  gid_t list[100];
  int i;
  int count = getgroups (sizeof (list) / sizeof (list[0]), list);
  if (count < 0)
    return 0;
  for (i = 0; i < count; i++)
    {
      gid_t id = list[i];
      struct group *details = getgrgid (id);
      if (details != NULL && strcmp (details->gr_passwd, "S-1-5-32-544") == 0)
        return 1;
    }
  return 0;
}
# define is_root() is_administrator ()
#else
# define is_root() (geteuid () == ROOT_UID)
#endif

static void
test_access (int (*func) (const char * /*file*/, int /*mode*/))
{
  /* Remove anything from prior partial run.  */
  unlink (BASE "f");
  unlink (BASE "f1");
  chmod (BASE "f2", 0600);
  unlink (BASE "f2");
  unlink (BASE "sl");

  {
    errno = 0;
    ASSERT (func (BASE "f", R_OK) == -1);
    ASSERT (errno == ENOENT);

    errno = 0;
    ASSERT (func (BASE "f", W_OK) == -1);
    ASSERT (errno == ENOENT);

    errno = 0;
    ASSERT (func (BASE "f", X_OK) == -1);
    ASSERT (errno == ENOENT);
  }
  {
    ASSERT (close (creat (BASE "f1", 0700)) == 0);

    ASSERT (func (BASE "f1", F_OK) == 0);
    ASSERT (func (BASE "f1", R_OK) == 0);
    ASSERT (func (BASE "f1", W_OK) == 0);
    ASSERT (func (BASE "f1", X_OK) == 0);

    ASSERT (func (BASE "f1/", F_OK) == -1);
    ASSERT (errno == ENOTDIR);
    ASSERT (func (BASE "f1/", R_OK) == -1);
    ASSERT (errno == ENOTDIR);
    ASSERT (func (BASE "f1/", W_OK) == -1);
    ASSERT (errno == ENOTDIR);
    ASSERT (func (BASE "f1/", X_OK) == -1);
    ASSERT (errno == ENOTDIR);

    if (symlink (BASE "f1", BASE "sl") == 0)
      {
        ASSERT (func (BASE "sl/", F_OK) == -1);
        ASSERT (errno == ENOTDIR);
        ASSERT (func (BASE "sl/", R_OK) == -1);
        ASSERT (errno == ENOTDIR);
        ASSERT (func (BASE "sl/", W_OK) == -1);
        ASSERT (errno == ENOTDIR);
        ASSERT (func (BASE "sl/", X_OK) == -1);
        ASSERT (errno == ENOTDIR);
      }
  }
  {
    ASSERT (close (creat (BASE "f2", 0600)) == 0);
    ASSERT (chmod (BASE "f2", 0400) == 0);

    ASSERT (func (BASE "f2", R_OK) == 0);

    /* On Cygwin, for users that are in the 'Administrators' group,
       W_OK is allowed.  */
    if (! is_root ())
      {
        errno = 0;
        ASSERT (func (BASE "f2", W_OK) == -1);
        ASSERT (errno == EACCES);
      }

#if defined _WIN32 && !defined __CYGWIN__
    /* X_OK works like R_OK.  */
    ASSERT (func (BASE "f2", X_OK) == 0);
#else
    /* On Solaris, for the root user, X_OK is allowed.
       On Cygwin 3.5.5, for users that are in the 'Administrators' group,
       X_OK is allowed.  */
# if defined __sun || defined __CYGWIN__
    if (! is_root ())
# endif
      {
        errno = 0;
        ASSERT (func (BASE "f2", X_OK) == -1);
        ASSERT (errno == EACCES);
      }
#endif
  }

  /* Cleanup.  */
  ASSERT (unlink (BASE "f1") == 0);
  ASSERT (chmod (BASE "f2", 0600) == 0);
  ASSERT (unlink (BASE "f2") == 0);
  unlink (BASE "sl");
}