Debian Codegraph @

vendor/ramsey/uuid/src/Provider/Dce/SystemDceSecurityProvider.php

raw
1 <?php
2
3 /**
4 * This file is part of the ramsey/uuid library
5 *
6 * For the full copyright and license information, please view the LICENSE
7 * file that was distributed with this source code.
8 *
9 * @copyright Copyright (c) Ben Ramsey <[email protected]>
10 * @license http://opensource.org/licenses/MIT MIT
11 */
12
13 declare(strict_types=1);
14
15 namespace Ramsey\Uuid\Provider\Dce;
16
17 use Ramsey\Uuid\Exception\DceSecurityException;
18 use Ramsey\Uuid\Provider\DceSecurityProviderInterface;
19 use Ramsey\Uuid\Type\Integer as IntegerObject;
20
21 use function escapeshellarg;
22 use function preg_split;
23 use function str_getcsv;
24 use function strrpos;
25 use function strtolower;
26 use function strtoupper;
27 use function substr;
28 use function trim;
29
30 use const PREG_SPLIT_NO_EMPTY;
31
32 /**
33 * SystemDceSecurityProvider retrieves the user or group identifiers from the system
34 */
35 class SystemDceSecurityProvider implements DceSecurityProviderInterface
36 {
37 /**
38 * @throws DceSecurityException if unable to get a user identifier
39 *
40 * @inheritDoc
41 */
42 public function getUid(): IntegerObject
43 {
44 /** @var IntegerObject | int | float | string | null $uid */
45 static $uid = null;
46
47 if ($uid instanceof IntegerObject) {
48 return $uid;
49 }
50
51 if ($uid === null) {
52 $uid = $this->getSystemUid();
53 }
54
55 if ($uid === '') {
56 throw new DceSecurityException(
57 'Unable to get a user identifier using the system DCE Security provider; please provide a custom '
58 . 'identifier or use a different provider',
59 );
60 }
61
62 $uid = new IntegerObject($uid);
63
64 return $uid;
65 }
66
67 /**
68 * @throws DceSecurityException if unable to get a group identifier
69 *
70 * @inheritDoc
71 */
72 public function getGid(): IntegerObject
73 {
74 /** @var IntegerObject | int | float | string | null $gid */
75 static $gid = null;
76
77 if ($gid instanceof IntegerObject) {
78 return $gid;
79 }
80
81 if ($gid === null) {
82 $gid = $this->getSystemGid();
83 }
84
85 if ($gid === '') {
86 throw new DceSecurityException(
87 'Unable to get a group identifier using the system DCE Security provider; please provide a custom '
88 . 'identifier or use a different provider',
89 );
90 }
91
92 $gid = new IntegerObject($gid);
93
94 return $gid;
95 }
96
97 /**
98 * Returns the UID from the system
99 */
100 private function getSystemUid(): string
101 {
102 if (!$this->hasShellExec()) {
103 return '';
104 }
105
106 return match ($this->getOs()) {
107 'WIN' => $this->getWindowsUid(),
108 default => trim((string) shell_exec('id -u')),
109 };
110 }
111
112 /**
113 * Returns the GID from the system
114 */
115 private function getSystemGid(): string
116 {
117 if (!$this->hasShellExec()) {
118 return '';
119 }
120
121 return match ($this->getOs()) {
122 'WIN' => $this->getWindowsGid(),
123 default => trim((string) shell_exec('id -g')),
124 };
125 }
126
127 /**
128 * Returns true if shell_exec() is available for use
129 */
130 private function hasShellExec(): bool
131 {
132 return !str_contains(strtolower((string) ini_get('disable_functions')), 'shell_exec');
133 }
134
135 /**
136 * Returns the PHP_OS string
137 */
138 private function getOs(): string
139 {
140 /** @var string $phpOs */
141 $phpOs = constant('PHP_OS');
142
143 return strtoupper(substr($phpOs, 0, 3));
144 }
145
146 /**
147 * Returns the user identifier for a user on a Windows system
148 *
149 * Windows does not have the same concept as an effective POSIX UID for the running script. Instead, each user is
150 * uniquely identified by an SID (security identifier). The SID includes three 32-bit unsigned integers that make up
151 * a unique domain identifier, followed by an RID (relative identifier) that we will use as the UID. The primary
152 * caveat is that this UID may not be unique to the system, since it is, instead, unique to the domain.
153 *
154 * @link https://www.lifewire.com/what-is-an-sid-number-2626005 What Is an SID Number?
155 * @link https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-dtyp/81d92bba-d22b-4a8c-908a-554ab29148ab Well-known SID Structures
156 * @link https://learn.microsoft.com/en-us/windows-server/identity/ad-ds/manage/understand-security-identifiers#well-known-sids Well-known SIDs
157 * @link https://www.windows-commandline.com/get-sid-of-user/ Get SID of user
158 */
159 private function getWindowsUid(): string
160 {
161 $response = shell_exec('whoami /user /fo csv /nh');
162
163 if ($response === null) {
164 return '';
165 }
166
167 $sid = str_getcsv(trim((string) $response), escape: '\\')[1] ?? '';
168
169 if (($lastHyphen = strrpos($sid, '-')) === false) {
170 return '';
171 }
172
173 return trim(substr($sid, $lastHyphen + 1));
174 }
175
176 /**
177 * Returns a group identifier for a user on a Windows system
178 *
179 * Since Windows does not have the same concept as an effective POSIX GID for the running script, we will get the
180 * local group memberships for the user running the script. Then, we will get the SID (security identifier) for the
181 * first group that appears in that list. Finally, we will return the RID (relative identifier) for the group and
182 * use that as the GID.
183 *
184 * @link https://www.windows-commandline.com/list-of-user-groups-command-line/ List of user groups command line
185 */
186 private function getWindowsGid(): string
187 {
188 $response = shell_exec('net user %username% | findstr /b /i "Local Group Memberships"');
189
190 if ($response === null) {
191 return '';
192 }
193
194 $userGroups = preg_split('/\s{2,}/', (string) $response, -1, PREG_SPLIT_NO_EMPTY);
195 $firstGroup = trim($userGroups[1] ?? '', "* \t\n\r\0\x0B");
196
197 if ($firstGroup === '') {
198 return '';
199 }
200
201 $response = shell_exec('wmic group get name,sid | findstr /b /i ' . escapeshellarg($firstGroup));
202
203 if ($response === null) {
204 return '';
205 }
206
207 $userGroup = preg_split('/\s{2,}/', (string) $response, -1, PREG_SPLIT_NO_EMPTY);
208 $sid = $userGroup[1] ?? '';
209
210 if (($lastHyphen = strrpos($sid, '-')) === false) {
211 return '';
212 }
213
214 return trim(substr($sid, $lastHyphen + 1));
215 }
216 }