| | | 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.DirectoryServices; |
| | | 6 | | using System.Runtime.InteropServices; |
| | | 7 | | using System.Security.Principal; |
| | | 8 | | using System.Xml; |
| | | 9 | | using CoreWCF.IdentityModel.Claims; |
| | | 10 | | using CoreWCF.Runtime; |
| | | 11 | | using CoreWCF.Security; |
| | | 12 | | |
| | | 13 | | namespace CoreWCF |
| | | 14 | | { |
| | | 15 | | public class SpnEndpointIdentity : EndpointIdentity |
| | | 16 | | { |
| | 0 | 17 | | private static TimeSpan s_spnLookupTime = TimeSpan.FromMinutes(1); |
| | | 18 | | private SecurityIdentifier _spnSid; |
| | | 19 | | |
| | | 20 | | // Double-checked locking pattern requires volatile for read/write synchronization |
| | | 21 | | private bool _hasSpnSidBeenComputed; |
| | 12 | 22 | | private readonly object _thisLock = new object(); |
| | 0 | 23 | | private static readonly object s_typeLock = new object(); |
| | | 24 | | |
| | | 25 | | // Double-checked locking pattern requires volatile for read/write synchronization |
| | | 26 | | private static DirectoryEntry s_directoryEntry; |
| | | 27 | | |
| | | 28 | | public static TimeSpan SpnLookupTime |
| | | 29 | | { |
| | | 30 | | get |
| | | 31 | | { |
| | 0 | 32 | | return s_spnLookupTime; |
| | | 33 | | } |
| | | 34 | | set |
| | | 35 | | { |
| | 0 | 36 | | if (value.Ticks < 0) |
| | | 37 | | { |
| | 0 | 38 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val |
| | 0 | 39 | | SRCommon.ValueMustBeNonNegative)); |
| | | 40 | | } |
| | 0 | 41 | | s_spnLookupTime = value; |
| | 0 | 42 | | } |
| | | 43 | | } |
| | | 44 | | |
| | 12 | 45 | | public SpnEndpointIdentity(string spnName) |
| | | 46 | | { |
| | 12 | 47 | | if (spnName == null) |
| | | 48 | | { |
| | 0 | 49 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(spnName)); |
| | | 50 | | } |
| | | 51 | | |
| | 12 | 52 | | Initialize(Claim.CreateSpnClaim(spnName)); |
| | 12 | 53 | | } |
| | | 54 | | |
| | 0 | 55 | | public SpnEndpointIdentity(Claim identity) |
| | | 56 | | { |
| | 0 | 57 | | if (identity == null) |
| | | 58 | | { |
| | 0 | 59 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(identity)); |
| | | 60 | | } |
| | | 61 | | |
| | 0 | 62 | | if (!ClaimTypes.Spn.Equals(identity.ClaimType)) |
| | | 63 | | { |
| | 0 | 64 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.Format(SR.UnrecognizedClaimTypeForIdenti |
| | | 65 | | } |
| | | 66 | | |
| | 0 | 67 | | Initialize(identity); |
| | 0 | 68 | | } |
| | | 69 | | |
| | | 70 | | internal override void WriteContentsTo(XmlDictionaryWriter writer) |
| | | 71 | | { |
| | 0 | 72 | | if (writer == null) |
| | | 73 | | { |
| | 0 | 74 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(writer)); |
| | | 75 | | } |
| | | 76 | | |
| | 0 | 77 | | writer.WriteElementString(XD.AddressingDictionary.Spn, XD.AddressingDictionary.IdentityExtensionNamespace, ( |
| | 0 | 78 | | } |
| | | 79 | | |
| | | 80 | | internal SecurityIdentifier GetSpnSid() |
| | | 81 | | { |
| | | 82 | | Fx.Assert(ClaimTypes.Spn.Equals(IdentityClaim.ClaimType) || ClaimTypes.Dns.Equals(IdentityClaim.ClaimType), |
| | 0 | 83 | | if (!_hasSpnSidBeenComputed) |
| | | 84 | | { |
| | 0 | 85 | | lock (_thisLock) |
| | | 86 | | { |
| | 0 | 87 | | if (!_hasSpnSidBeenComputed) |
| | | 88 | | { |
| | 0 | 89 | | string spn = null; |
| | | 90 | | try |
| | | 91 | | { |
| | 0 | 92 | | if (ClaimTypes.Dns.Equals(IdentityClaim.ClaimType)) |
| | | 93 | | { |
| | 0 | 94 | | spn = "host/" + (string)IdentityClaim.Resource; |
| | | 95 | | } |
| | | 96 | | else |
| | | 97 | | { |
| | 0 | 98 | | spn = (string)IdentityClaim.Resource; |
| | | 99 | | } |
| | | 100 | | // canonicalize SPN for use in LDAP filter following RFC 1960: |
| | 0 | 101 | | if (spn != null) |
| | | 102 | | { |
| | 0 | 103 | | spn = spn.Replace("*", @"\*").Replace("(", @"\(").Replace(")", @"\)"); |
| | | 104 | | } |
| | | 105 | | |
| | 0 | 106 | | DirectoryEntry de = GetDirectoryEntry(); |
| | 0 | 107 | | using (DirectorySearcher searcher = new DirectorySearcher(de)) |
| | | 108 | | { |
| | 0 | 109 | | searcher.CacheResults = true; |
| | 0 | 110 | | searcher.ClientTimeout = SpnLookupTime; |
| | 0 | 111 | | searcher.Filter = "(&(objectCategory=Computer)(objectClass=computer)(servicePrincipalNam |
| | 0 | 112 | | searcher.PropertiesToLoad.Add("objectSid"); |
| | 0 | 113 | | SearchResult result = searcher.FindOne(); |
| | 0 | 114 | | if (result != null) |
| | | 115 | | { |
| | 0 | 116 | | byte[] sidBinaryForm = (byte[])result.Properties["objectSid"][0]; |
| | 0 | 117 | | _spnSid = new SecurityIdentifier(sidBinaryForm, 0); |
| | | 118 | | } |
| | | 119 | | else |
| | | 120 | | { |
| | | 121 | | //SecurityTraceRecordHelper.TraceSpnToSidMappingFailure(spn, null); |
| | | 122 | | } |
| | 0 | 123 | | } |
| | 0 | 124 | | } |
| | | 125 | | #pragma warning disable 56500 // covered by FxCOP |
| | 0 | 126 | | catch (Exception e) |
| | | 127 | | { |
| | | 128 | | // Always immediately rethrow fatal exceptions. |
| | 0 | 129 | | if (Fx.IsFatal(e)) |
| | | 130 | | { |
| | 0 | 131 | | throw; |
| | | 132 | | } |
| | | 133 | | |
| | 0 | 134 | | if (e is NullReferenceException || e is SEHException) |
| | | 135 | | { |
| | 0 | 136 | | throw; |
| | | 137 | | } |
| | | 138 | | |
| | | 139 | | //SecurityTraceRecordHelper.TraceSpnToSidMappingFailure(spn, e); |
| | 0 | 140 | | } |
| | | 141 | | finally |
| | | 142 | | { |
| | 0 | 143 | | _hasSpnSidBeenComputed = true; |
| | 0 | 144 | | } |
| | | 145 | | } |
| | 0 | 146 | | } |
| | | 147 | | } |
| | 0 | 148 | | return _spnSid; |
| | | 149 | | } |
| | | 150 | | |
| | | 151 | | private static DirectoryEntry GetDirectoryEntry() |
| | | 152 | | { |
| | 0 | 153 | | if (s_directoryEntry == null) |
| | | 154 | | { |
| | 0 | 155 | | lock (s_typeLock) |
| | | 156 | | { |
| | 0 | 157 | | if (s_directoryEntry == null) |
| | | 158 | | { |
| | 0 | 159 | | DirectoryEntry tmp = new DirectoryEntry(@"LDAP://" + SecurityUtils.GetPrimaryDomain()); |
| | 0 | 160 | | tmp.RefreshCache(new string[] { "name" }); |
| | 0 | 161 | | s_directoryEntry = tmp; |
| | | 162 | | } |
| | 0 | 163 | | } |
| | | 164 | | } |
| | 0 | 165 | | return s_directoryEntry; |
| | | 166 | | } |
| | | 167 | | } |
| | | 168 | | } |