| | | 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.Collections.ObjectModel; |
| | | 5 | | using System.IO; |
| | | 6 | | using System.Net.Sockets; |
| | | 7 | | using System.Security.Claims; |
| | | 8 | | using System.Security.Principal; |
| | | 9 | | using System.Threading; |
| | | 10 | | using System.Threading.Tasks; |
| | | 11 | | using CoreWCF.Channels.Framing; |
| | | 12 | | using CoreWCF.IdentityModel; |
| | | 13 | | using CoreWCF.IdentityModel.Policy; |
| | | 14 | | using CoreWCF.IdentityModel.Selectors; |
| | | 15 | | using CoreWCF.IdentityModel.Tokens; |
| | | 16 | | using CoreWCF.Security; |
| | | 17 | | using CoreWCF.UnixDomainSocket.Security; |
| | | 18 | | using Microsoft.AspNetCore.Connections.Features; |
| | | 19 | | |
| | | 20 | | namespace CoreWCF.Channels |
| | | 21 | | { |
| | | 22 | | internal class UnixPosixIdentitySecurityUpgradeProvider : StreamSecurityUpgradeProvider |
| | | 23 | | { |
| | | 24 | | public UnixPosixIdentitySecurityUpgradeProvider(UnixPosixIdentityBindingElement bindingElement, BindingContext c |
| | 2 | 25 | | : base(context.Binding) |
| | | 26 | | { |
| | 2 | 27 | | Scheme = context.Binding.Scheme; |
| | 2 | 28 | | } |
| | | 29 | | |
| | 0 | 30 | | public string Scheme { get; } |
| | | 31 | | |
| | 1 | 32 | | public override EndpointIdentity Identity => null; |
| | | 33 | | |
| | | 34 | | public override StreamUpgradeAcceptor CreateUpgradeAcceptor() |
| | | 35 | | { |
| | 2 | 36 | | ThrowIfDisposedOrNotOpen(); |
| | 2 | 37 | | return new UnixPosixIdentitySecurityUpgradeAcceptor(this); |
| | | 38 | | } |
| | | 39 | | |
| | | 40 | | protected override void OnAbort() |
| | | 41 | | { |
| | 0 | 42 | | } |
| | | 43 | | |
| | | 44 | | protected override Task OnCloseAsync(CancellationToken token) |
| | | 45 | | { |
| | 1 | 46 | | return Task.CompletedTask; |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | protected override Task OnOpenAsync(CancellationToken token) |
| | | 50 | | { |
| | 2 | 51 | | return Task.CompletedTask; |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | protected override void OnOpened() |
| | | 55 | | { |
| | 2 | 56 | | base.OnOpened(); |
| | 2 | 57 | | } |
| | | 58 | | |
| | | 59 | | internal class UnixPosixIdentitySecurityUpgradeAcceptor : StreamSecurityUpgradeAcceptor |
| | | 60 | | { |
| | | 61 | | private readonly UnixPosixIdentitySecurityUpgradeProvider _parent; |
| | | 62 | | private SecurityMessageProperty _remoteSecurity; |
| | | 63 | | private bool _securityUpgraded; |
| | | 64 | | private Socket _socket; |
| | | 65 | | private readonly string _upgradeString; |
| | 2 | 66 | | public UnixPosixIdentitySecurityUpgradeAcceptor(UnixPosixIdentitySecurityUpgradeProvider parent) |
| | | 67 | | { |
| | 2 | 68 | | _parent = parent; |
| | 2 | 69 | | _upgradeString = "application/unixposix"; |
| | 2 | 70 | | } |
| | | 71 | | |
| | | 72 | | |
| | | 73 | | public override async Task<Stream> AcceptUpgradeAsync(Stream stream) |
| | | 74 | | { |
| | 1 | 75 | | FramingConnection conn = this.Features.Get<FramingConnection>(); |
| | 1 | 76 | | _socket = conn.ConnectionFeatures.Get<IConnectionSocketFeature>().Socket; |
| | 1 | 77 | | if (stream == null) |
| | | 78 | | { |
| | 0 | 79 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(stream)); |
| | | 80 | | } |
| | | 81 | | |
| | 1 | 82 | | (stream, _remoteSecurity) = await OnAcceptUpgradeAsync(stream); |
| | 1 | 83 | | _securityUpgraded = true; |
| | 1 | 84 | | return stream; |
| | 1 | 85 | | } |
| | | 86 | | |
| | | 87 | | public override bool CanUpgrade(string contentType) |
| | | 88 | | { |
| | 1 | 89 | | if (_securityUpgraded) |
| | | 90 | | { |
| | 0 | 91 | | return false; |
| | | 92 | | } |
| | | 93 | | |
| | 1 | 94 | | return (contentType == _upgradeString); |
| | | 95 | | } |
| | | 96 | | |
| | | 97 | | public override SecurityMessageProperty GetRemoteSecurity() |
| | | 98 | | { |
| | 2 | 99 | | return _remoteSecurity; |
| | | 100 | | } |
| | | 101 | | |
| | | 102 | | protected async Task<(Stream, SecurityMessageProperty)> OnAcceptUpgradeAsync(Stream stream) |
| | | 103 | | { |
| | | 104 | | |
| | 1 | 105 | | SecurityMessageProperty remoteSecurity = await CreateClientSecurityAsync(); |
| | 1 | 106 | | return (stream, remoteSecurity); |
| | 1 | 107 | | } |
| | | 108 | | |
| | | 109 | | private async Task<SecurityMessageProperty> CreateClientSecurityAsync() |
| | | 110 | | { |
| | | 111 | | |
| | 1 | 112 | | if(!_socket.TryGetCredentials(out uint processId, out uint userId, out uint groupId)) |
| | | 113 | | { |
| | 0 | 114 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityNegotiationException(SR.Authen |
| | | 115 | | } |
| | 1 | 116 | | GenericIdentity genericIdentity = null; |
| | 1 | 117 | | if (userId > 0) |
| | | 118 | | { |
| | 1 | 119 | | UserInfo userInfo = NativeSysCall.GetUserInfo(userId); |
| | 1 | 120 | | GroupInfo groupInfo = NativeSysCall.GetGroupInfo(groupId); |
| | 1 | 121 | | if (userInfo == null || groupInfo == null) |
| | | 122 | | { |
| | 0 | 123 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityNegotiationException(SR.Au |
| | | 124 | | } |
| | 1 | 125 | | genericIdentity = new GenericIdentity(userInfo.Name); |
| | 1 | 126 | | ClaimsIdentity claimsIdentity = new ClaimsIdentity(genericIdentity); |
| | | 127 | | //should we add member of (Group) ? |
| | 1 | 128 | | SecurityUtils.AddPosixClaims(claimsIdentity, groupInfo.Name, groupInfo.Id, processId); |
| | | 129 | | } |
| | 0 | 130 | | else if(processId>0) |
| | | 131 | | { |
| | 0 | 132 | | genericIdentity = new GenericIdentity(processId.ToString()); |
| | 0 | 133 | | ClaimsIdentity claimsIdentity = new ClaimsIdentity(genericIdentity); |
| | 0 | 134 | | SecurityUtils.AddProcessIdClaim(claimsIdentity, processId); |
| | | 135 | | } |
| | 1 | 136 | | SecurityToken token = new GenericIdentitySecurityToken(genericIdentity, SecurityUniqueId.Create().Value) |
| | 1 | 137 | | WindowsSecurityTokenAuthenticator authenticator = new WindowsSecurityTokenAuthenticator(); |
| | 1 | 138 | | ReadOnlyCollection<IAuthorizationPolicy> authorizationPolicies = await authenticator.ValidateTokenAsync |
| | 1 | 139 | | SecurityMessageProperty clientSecurity = new SecurityMessageProperty |
| | 1 | 140 | | { |
| | 1 | 141 | | TransportToken = new SecurityTokenSpecification(token, authorizationPolicies), |
| | 1 | 142 | | ServiceSecurityContext = new ServiceSecurityContext(authorizationPolicies) |
| | 1 | 143 | | }; |
| | 1 | 144 | | return clientSecurity; |
| | 1 | 145 | | } |
| | | 146 | | } |
| | | 147 | | } |
| | | 148 | | } |