| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Buffers; |
| | | 6 | | using System.Collections.Generic; |
| | | 7 | | using System.Net.Sockets; |
| | | 8 | | using System.Runtime.InteropServices; |
| | | 9 | | |
| | | 10 | | namespace CoreWCF.UnixDomainSocket.Security |
| | | 11 | | { |
| | | 12 | | internal static class UnixDomainSocketInterop |
| | | 13 | | { |
| | | 14 | | |
| | | 15 | | internal static bool TryGetCredentials(this Socket socket, out uint processId, out uint userId, out uint groupId |
| | | 16 | | { |
| | | 17 | | try |
| | | 18 | | { |
| | | 19 | | if (OperatingSystem.IsWindows()) |
| | | 20 | | throw new PlatformNotSupportedException(); |
| | | 21 | | |
| | | 22 | | Span<uint> ucred = stackalloc uint[3]; |
| | | 23 | | int bytesWritten; |
| | | 24 | | if (OperatingSystem.IsMacOS()) |
| | | 25 | | { |
| | | 26 | | bytesWritten = socket.GetRawSocketOption(0, 2, MemoryMarshal.AsBytes(ucred)); // get processid |
| | | 27 | | processId = ucred[0]; |
| | | 28 | | userId = 0; |
| | | 29 | | groupId = 0; |
| | | 30 | | return true; |
| | | 31 | | } |
| | | 32 | | else |
| | | 33 | | { |
| | | 34 | | const int SOL_SOCKET = 1; |
| | | 35 | | const int SO_PEERCRED = 17; |
| | | 36 | | bytesWritten = socket.GetRawSocketOption(SOL_SOCKET, SO_PEERCRED, MemoryMarshal.AsBytes(ucred)); |
| | | 37 | | } |
| | | 38 | | processId = ucred[0]; |
| | | 39 | | userId = ucred[1]; |
| | | 40 | | groupId = ucred[2]; |
| | | 41 | | return bytesWritten == (ucred.Length * sizeof(uint)); |
| | | 42 | | } |
| | | 43 | | catch(Exception ex) |
| | | 44 | | { |
| | | 45 | | throw; |
| | | 46 | | } |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | internal static class NativeSysCall |
| | | 52 | | { |
| | | 53 | | private const string LIBC = "libc"; |
| | | 54 | | private const int InitialBufferSize = 1024; |
| | | 55 | | // Hard upper bound to avoid unbounded growth on a misbehaving NSS implementation. |
| | | 56 | | private const int MaxBufferSize = 16 * 1024 * 1024; |
| | | 57 | | // POSIX errno value for "buffer too small". Both Linux and macOS use 34. |
| | | 58 | | private const int ERANGE = 34; |
| | | 59 | | |
| | | 60 | | [DllImport(LIBC, SetLastError = false)] |
| | | 61 | | private static extern unsafe int getpwuid_r(uint uid, PasswdRaw* pwd, byte* buf, nuint buflen, PasswdRaw** resul |
| | | 62 | | |
| | | 63 | | [DllImport(LIBC, SetLastError = false)] |
| | | 64 | | private static extern unsafe int getgrgid_r(uint gid, GroupRaw* grp, byte* buf, nuint buflen, GroupRaw** result) |
| | | 65 | | |
| | | 66 | | [StructLayout(LayoutKind.Sequential)] |
| | | 67 | | private unsafe struct PasswdRaw |
| | | 68 | | { |
| | | 69 | | public byte* pw_name; |
| | | 70 | | public byte* pw_passwd; |
| | | 71 | | public uint pw_uid; |
| | | 72 | | public uint pw_gid; |
| | | 73 | | public byte* pw_gecos; |
| | | 74 | | public byte* pw_dir; |
| | | 75 | | public byte* pw_shell; |
| | | 76 | | } |
| | | 77 | | |
| | | 78 | | [StructLayout(LayoutKind.Sequential)] |
| | | 79 | | private unsafe struct GroupRaw |
| | | 80 | | { |
| | | 81 | | public byte* gr_name; |
| | | 82 | | public byte* gr_passwd; |
| | | 83 | | public uint gr_gid; |
| | | 84 | | // gr_mem is char**: a NULL-terminated array of char*. |
| | | 85 | | public byte** gr_mem; |
| | | 86 | | } |
| | | 87 | | |
| | | 88 | | internal static UserInfo GetUserInfo(uint uid) |
| | | 89 | | { |
| | 982953 | 90 | | int bufSize = InitialBufferSize; |
| | | 91 | | while (true) |
| | | 92 | | { |
| | 982953 | 93 | | byte[] buf = ArrayPool<byte>.Shared.Rent(bufSize); |
| | | 94 | | try |
| | | 95 | | { |
| | 982953 | 96 | | int err = ReadUserInfo(uid, buf, out UserInfo info, out bool found); |
| | 982953 | 97 | | if (err == 0) |
| | | 98 | | { |
| | 982953 | 99 | | return found ? info : null; |
| | | 100 | | } |
| | 0 | 101 | | if (err == ERANGE && bufSize < MaxBufferSize) |
| | | 102 | | { |
| | 0 | 103 | | bufSize = Math.Min(bufSize * 2, MaxBufferSize); |
| | 0 | 104 | | continue; |
| | | 105 | | } |
| | 0 | 106 | | return null; |
| | | 107 | | } |
| | | 108 | | finally |
| | | 109 | | { |
| | 982953 | 110 | | ArrayPool<byte>.Shared.Return(buf); |
| | 982953 | 111 | | } |
| | | 112 | | } |
| | 982953 | 113 | | } |
| | | 114 | | |
| | | 115 | | internal static GroupInfo GetGroupInfo(uint gid) |
| | | 116 | | { |
| | 907975 | 117 | | int bufSize = InitialBufferSize; |
| | | 118 | | while (true) |
| | | 119 | | { |
| | 907975 | 120 | | byte[] buf = ArrayPool<byte>.Shared.Rent(bufSize); |
| | | 121 | | try |
| | | 122 | | { |
| | 907975 | 123 | | int err = ReadGroupInfo(gid, buf, out GroupInfo info, out bool found); |
| | 907975 | 124 | | if (err == 0) |
| | | 125 | | { |
| | 907975 | 126 | | return found ? info : null; |
| | | 127 | | } |
| | 0 | 128 | | if (err == ERANGE && bufSize < MaxBufferSize) |
| | | 129 | | { |
| | 0 | 130 | | bufSize = Math.Min(bufSize * 2, MaxBufferSize); |
| | 0 | 131 | | continue; |
| | | 132 | | } |
| | 0 | 133 | | return null; |
| | | 134 | | } |
| | | 135 | | finally |
| | | 136 | | { |
| | 907975 | 137 | | ArrayPool<byte>.Shared.Return(buf); |
| | 907975 | 138 | | } |
| | | 139 | | } |
| | 907975 | 140 | | } |
| | | 141 | | |
| | | 142 | | private static unsafe int ReadUserInfo(uint uid, byte[] buf, out UserInfo info, out bool found) |
| | | 143 | | { |
| | 982953 | 144 | | info = null; |
| | 982953 | 145 | | found = false; |
| | | 146 | | PasswdRaw pwd; |
| | | 147 | | PasswdRaw* result; |
| | | 148 | | int err; |
| | 982953 | 149 | | fixed (byte* pBuf = buf) |
| | | 150 | | { |
| | 982953 | 151 | | err = getpwuid_r(uid, &pwd, pBuf, (nuint)buf.Length, &result); |
| | 982953 | 152 | | if (err != 0 || result == null) |
| | | 153 | | { |
| | 0 | 154 | | return err; |
| | | 155 | | } |
| | 982953 | 156 | | string name = Marshal.PtrToStringAnsi((IntPtr)pwd.pw_name); |
| | 982953 | 157 | | if (name == null) |
| | | 158 | | { |
| | 0 | 159 | | return 0; |
| | | 160 | | } |
| | 982953 | 161 | | string directory = Marshal.PtrToStringAnsi((IntPtr)pwd.pw_dir); |
| | 982953 | 162 | | string shell = Marshal.PtrToStringAnsi((IntPtr)pwd.pw_shell); |
| | 982953 | 163 | | info = new UserInfo(name, pwd.pw_uid, pwd.pw_gid, directory, shell); |
| | 982953 | 164 | | found = true; |
| | 982953 | 165 | | return 0; |
| | | 166 | | } |
| | | 167 | | } |
| | | 168 | | |
| | | 169 | | private static unsafe int ReadGroupInfo(uint gid, byte[] buf, out GroupInfo info, out bool found) |
| | | 170 | | { |
| | 907975 | 171 | | info = null; |
| | 907975 | 172 | | found = false; |
| | | 173 | | GroupRaw grp; |
| | | 174 | | GroupRaw* result; |
| | | 175 | | int err; |
| | 907975 | 176 | | fixed (byte* pBuf = buf) |
| | | 177 | | { |
| | 907975 | 178 | | err = getgrgid_r(gid, &grp, pBuf, (nuint)buf.Length, &result); |
| | 907975 | 179 | | if (err != 0 || result == null) |
| | | 180 | | { |
| | 0 | 181 | | return err; |
| | | 182 | | } |
| | 907975 | 183 | | string name = Marshal.PtrToStringAnsi((IntPtr)grp.gr_name); |
| | 907975 | 184 | | if (name == null) |
| | | 185 | | { |
| | 0 | 186 | | return 0; |
| | | 187 | | } |
| | 907975 | 188 | | string[] members = MaterializeMembers(grp.gr_mem); |
| | 907975 | 189 | | info = new GroupInfo(name, grp.gr_gid, members); |
| | 907975 | 190 | | found = true; |
| | 907975 | 191 | | return 0; |
| | | 192 | | } |
| | | 193 | | } |
| | | 194 | | |
| | | 195 | | private static unsafe string[] MaterializeMembers(byte** gr_mem) |
| | | 196 | | { |
| | 907975 | 197 | | if (gr_mem == null) |
| | | 198 | | { |
| | 0 | 199 | | return Array.Empty<string>(); |
| | | 200 | | } |
| | 907975 | 201 | | var list = new List<string>(); |
| | 1815950 | 202 | | for (int i = 0; gr_mem[i] != null; i++) |
| | | 203 | | { |
| | 0 | 204 | | string member = Marshal.PtrToStringAnsi((IntPtr)gr_mem[i]); |
| | 0 | 205 | | if (member != null) |
| | | 206 | | { |
| | 0 | 207 | | list.Add(member); |
| | | 208 | | } |
| | | 209 | | } |
| | 907975 | 210 | | return list.ToArray(); |
| | | 211 | | } |
| | | 212 | | } |
| | | 213 | | |
| | | 214 | | internal sealed class GroupInfo |
| | | 215 | | { |
| | | 216 | | public string Name { get; } |
| | | 217 | | public uint Id { get; } |
| | | 218 | | public string[] Members { get; } |
| | | 219 | | |
| | | 220 | | internal GroupInfo(string name, uint id, string[] members) |
| | | 221 | | { |
| | | 222 | | Name = name; |
| | | 223 | | Id = id; |
| | | 224 | | Members = members ?? Array.Empty<string>(); |
| | | 225 | | } |
| | | 226 | | } |
| | | 227 | | |
| | | 228 | | internal class UserInfo |
| | | 229 | | { |
| | | 230 | | public string Name { get; } |
| | | 231 | | public uint Uid { get; } |
| | | 232 | | public uint Gid { get; } |
| | | 233 | | public string Directory { get; } |
| | | 234 | | public string Shell { get; } |
| | | 235 | | |
| | | 236 | | internal UserInfo(string name, uint uid, uint gid, string directory, string shell) |
| | | 237 | | { |
| | | 238 | | Name = name; |
| | | 239 | | Uid = uid; |
| | | 240 | | Gid = gid; |
| | | 241 | | Directory = directory; |
| | | 242 | | Shell = shell; |
| | | 243 | | } |
| | | 244 | | } |
| | | 245 | | } |