未验证 提交 f99ebea4 编写于 作者: T Tom Deseyn 提交者: GitHub

NamedPipeServerStream.Unix: GetImpersonationUserName tweaks. (#88125)

* NamedPipeServerStream.Unix: GetImpersonationUserName tweaks.

When the user name is not found, return an empty string like Environment.UserName.

Add EINTR handling.

* Remove GetNameFromUid and use GetUserNameFromPasswd.
上级 cfae69f6
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
internal static partial class Interop
{
internal static partial class Sys
{
[LibraryImport(Libraries.SystemNative, EntryPoint = "SystemNative_GetPeerUserName", StringMarshalling = StringMarshalling.Utf8, SetLastError = true)]
internal static partial string GetPeerUserName(SafeHandle socket);
}
}
......@@ -145,14 +145,14 @@
Link="Common\Interop\Unix\Interop.FLock.cs" />
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.GetHostName.cs"
Link="Common\Interop\Unix\Interop.GetHostName.cs" />
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.GetPeerUserName.cs"
Link="Common\Interop\Unix\Interop.GetPeerUserName.cs" />
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.Open.cs"
Link="Common\Interop\Unix\Interop.Open.cs" />
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.OpenFlags.cs"
Link="Common\Interop\Unix\Interop.OpenFlags.cs" />
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.Pipe.cs"
Link="Common\Interop\Unix\Interop.Pipe.cs" />
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.GetPwUid.cs"
Link="Common\Interop\Unix\System.Native\Interop.GetPwUid.cs" />
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.Read.Pipe.cs"
Link="Common\Interop\Unix\Interop.Read.Pipe.cs" />
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.Unlink.cs"
......
......@@ -167,13 +167,13 @@ public string GetImpersonationUserName()
throw new InvalidOperationException(SR.InvalidOperation_PipeHandleNotSet);
}
string name = Interop.Sys.GetPeerUserName(handle);
if (name != null)
uint peerID;
if (Interop.Sys.GetPeerID(handle, out peerID) == -1)
{
return name;
throw CreateExceptionForLastError(_instance?.PipeName);
}
throw CreateExceptionForLastError(_instance?.PipeName);
return Interop.Sys.GetUserNameFromPasswd(peerID);
}
public override int InBufferSize
......
......@@ -183,7 +183,6 @@ static const Entry s_sysNative[] =
DllImportEntry(SystemNative_TryChangeSocketEventRegistration)
DllImportEntry(SystemNative_WaitForSocketEvents)
DllImportEntry(SystemNative_PlatformSupportsDualModeIPv4PacketInfo)
DllImportEntry(SystemNative_GetPeerUserName)
DllImportEntry(SystemNative_GetDomainSocketSizes)
DllImportEntry(SystemNative_GetMaximumAddressSize)
DllImportEntry(SystemNative_SendFile)
......
......@@ -3036,51 +3036,6 @@ int32_t SystemNative_PlatformSupportsDualModeIPv4PacketInfo(void)
#endif
}
static char* GetNameFromUid(uid_t uid)
{
size_t bufferLength = 512;
while (1)
{
char *buffer = (char*)malloc(bufferLength);
if (buffer == NULL)
return NULL;
struct passwd pw;
struct passwd* result;
if (getpwuid_r(uid, &pw, buffer, bufferLength, &result) == 0)
{
if (result == NULL)
{
errno = ENOENT;
free(buffer);
return NULL;
}
else
{
char* name = strdup(pw.pw_name);
free(buffer);
return name;
}
}
free(buffer);
size_t tmpBufferLength;
if (errno != ERANGE || !multiply_s(bufferLength, (size_t)2, &tmpBufferLength))
{
return NULL;
}
bufferLength = tmpBufferLength;
}
}
char* SystemNative_GetPeerUserName(intptr_t socket)
{
uid_t euid;
return SystemNative_GetPeerID(socket, &euid) == 0 ?
GetNameFromUid(euid) :
NULL;
}
void SystemNative_GetDomainSocketSizes(int32_t* pathOffset, int32_t* pathSize, int32_t* addressSize)
{
assert(pathOffset != NULL);
......
......@@ -411,8 +411,6 @@ PALEXPORT int32_t SystemNative_WaitForSocketEvents(intptr_t port, SocketEvent* b
PALEXPORT int32_t SystemNative_PlatformSupportsDualModeIPv4PacketInfo(void);
PALEXPORT char* SystemNative_GetPeerUserName(intptr_t socket);
PALEXPORT void SystemNative_GetDomainSocketSizes(int32_t* pathOffset, int32_t* pathSize, int32_t* addressSize);
PALEXPORT int32_t SystemNative_GetMaximumAddressSize(void);
......
......@@ -411,12 +411,6 @@ int32_t SystemNative_PlatformSupportsDualModeIPv4PacketInfo(void)
return 0;
}
char* SystemNative_GetPeerUserName(intptr_t socket)
{
return NULL;
}
void SystemNative_GetDomainSocketSizes(int32_t* pathOffset, int32_t* pathSize, int32_t* addressSize)
{
*pathOffset = -1;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册