private static void CheckAndUpdateSegment()
 {
     Canary15DataSegment.SampleUtcNow();
     if (Canary15DataSegment.UtcNow >= Canary15DataManager.NextRefreshTime)
     {
         Canary15DataManager.ResetNextRefreshTime();
         Canary15DataManager.RecalculateState();
         if (Canary15DataManager.pendingSegment != null)
         {
             Canary15DataManager.pendingSegment.Trace(8, "CheckAndUpdateSegment()");
             if (Canary15DataManager.pendingSegment.State == Canary15DataSegment.SegmentState.Pending && Canary15DataManager.pendingSegment.NextRefreshTime < Canary15DataSegment.UtcNow)
             {
                 Canary15DataSegment.LoadClientAccessADObject();
                 Canary15DataManager.pendingSegment.ReadSegmentFromAD();
                 Canary15DataManager.NextRefreshTime = Canary15DataManager.pendingSegment.NextRefreshTime;
                 Canary15DataManager.pendingSegment.Trace(8, "CheckAndUpdateSegment()");
                 Canary15DataManager.pendingSegment.LogToIIS(8);
                 return;
             }
         }
         else if (Canary15DataManager.oldSegment != null)
         {
             Canary15DataManager.oldSegment.Trace(8, "CheckAndUpdateSegment()");
             Canary15DataManager.oldSegment.LogToIIS(8);
             Canary15DataManager.activeSegment.Trace(8, "CheckAndUpdateSegment()");
             Canary15DataManager.activeSegment.LogToIIS(8);
             Canary15DataManager.oldSegment.CloneFromSegment(Canary15DataManager.activeSegment);
             Canary15DataManager.oldSegment.Trace(8, "CheckAndUpdateSegment()");
             Canary15DataManager.oldSegment.LogToIIS(8);
             Canary15DataManager.oldSegment.SaveSegmentToAD();
             Canary15DataManager.NextRefreshTime = Canary15DataManager.oldSegment.NextRefreshTime;
         }
     }
 }
Esempio n. 2
0
        public static bool ValidateCanary15(string canaryString, string logonUniqueKey)
        {
            byte[] userContextIdBinary;
            byte[] array;
            byte[] array2;
            if (!Canary15.ParseCanary15(canaryString, out userContextIdBinary, out array, out array2))
            {
                ExTraceGlobals.CoreTracer.TraceDebug <string>(10L, "ValidateCanary failed, canaryString={0}", canaryString);
                return(false);
            }
            if (Canary15.IsExpired(array))
            {
                ExTraceGlobals.CoreTracer.TraceDebug <string>(10L, "Canary is expired, timeStampBinary={0}", Canary15.GetHexString(array));
                return(false);
            }
            long num;
            int  num2;

            byte[] array3 = Canary15DataManager.ComputeHash(userContextIdBinary, array, logonUniqueKey, out num, out num2);
            if (Canary15.AreEqual(array2, array3))
            {
                return(true);
            }
            ExTraceGlobals.CoreTracer.TraceDebug <string, string>(10L, "testHashBinary={0}!=hashBinary={1}", Canary15.GetHexString(array3), Canary15.GetHexString(array2));
            return(false);
        }
        public static byte[] ComputeHash(byte[] userContextIdBinary, byte[] timestampBinary, string logOnUniqueKey, out long keyIndex, out int segmentIndex)
        {
            long ticks = BitConverter.ToInt64(timestampBinary, 0);

            byte[] array;
            if (Canary15DataManager.GetEntry(ticks, out array, out keyIndex, out segmentIndex))
            {
                byte[] bytes = new UnicodeEncoding().GetBytes(logOnUniqueKey);
                int    num   = userContextIdBinary.Length + timestampBinary.Length + bytes.Length;
                num += array.Length;
                byte[] array2 = new byte[num];
                int    num2   = 0;
                userContextIdBinary.CopyTo(array2, num2);
                num2 += userContextIdBinary.Length;
                timestampBinary.CopyTo(array2, num2);
                num2 += timestampBinary.Length;
                bytes.CopyTo(array2, num2);
                num2 += bytes.Length;
                array.CopyTo(array2, num2);
                byte[] result;
                using (SHA256Cng sha256Cng = new SHA256Cng())
                {
                    result = sha256Cng.ComputeHash(array2);
                    sha256Cng.Clear();
                }
                return(result);
            }
            return(null);
        }
Esempio n. 4
0
        public Canary15(string logonUniqueKey)
        {
            byte[] userContextIdBinary = Guid.NewGuid().ToByteArray();
            byte[] bytes = BitConverter.GetBytes(DateTime.UtcNow.Ticks);
            long   keyIndex;
            int    segmentIndex;

            byte[] hashBinary = Canary15DataManager.ComputeHash(userContextIdBinary, bytes, logonUniqueKey, out keyIndex, out segmentIndex);
            this.Init(userContextIdBinary, bytes, logonUniqueKey, hashBinary, Canary15.FormatLogData(keyIndex, segmentIndex));
            this.IsRenewed       = true;
            this.IsAboutToExpire = false;
        }
        public static bool GetEntry(long ticks, out byte[] key, out long keyIndex, out int segment)
        {
            bool result;

            lock (Canary15DataManager.segments)
            {
                Canary15DataManager.CheckAndUpdateSegment();
                if (Canary15DataManager.activeSegment != null && Canary15DataManager.activeSegment.FindEntry(ticks, out key, out keyIndex))
                {
                    segment = Canary15DataManager.activeSegment.SegmentIndex;
                    result  = true;
                }
                else if (Canary15DataManager.historySegment != null && Canary15DataManager.historySegment.FindEntry(ticks, out key, out keyIndex))
                {
                    segment = Canary15DataManager.historySegment.SegmentIndex;
                    result  = true;
                }
                else
                {
                    key      = Canary15DataSegment.BackupKey;
                    keyIndex = -2L;
                    segment  = -2;
                    if (Canary15DataManager.traceEnableCounter > 0)
                    {
                        Canary15DataManager.traceEnableCounter--;
                        new DateTime(ticks, DateTimeKind.Utc);
                        Canary15Trace.LogToIIS("Canary.T" + Canary15DataManager.traceEnableCounter, ticks.ToString());
                        if (Canary15DataManager.activeSegment != null)
                        {
                            Canary15DataManager.activeSegment.LogToIIS(9);
                            Canary15DataManager.activeSegment.Trace(9, "GetEntry()");
                        }
                        if (Canary15DataManager.historySegment != null)
                        {
                            Canary15DataManager.historySegment.LogToIIS(9);
                            Canary15DataManager.historySegment.Trace(9, "GetEntry()");
                        }
                        if (Canary15DataManager.pendingSegment != null)
                        {
                            Canary15DataManager.pendingSegment.LogToIIS(9);
                            Canary15DataManager.pendingSegment.Trace(9, "GetEntry()");
                        }
                    }
                    result = true;
                }
            }
            return(result);
        }
Esempio n. 6
0
 public static Canary15 RestoreCanary15(string canaryString, string logonUniqueKey)
 {
     byte[] userContextIdBinary;
     byte[] array;
     byte[] array2;
     if (Canary15.ParseCanary15(canaryString, out userContextIdBinary, out array, out array2))
     {
         if (Canary15.IsExpired(array))
         {
             ExTraceGlobals.CoreTracer.TraceDebug <string>(5L, "Canary is expired, timeStampBinary={0}", Canary15.GetHexString(array));
             return(null);
         }
         long   keyIndex;
         int    segmentIndex;
         byte[] array3 = Canary15DataManager.ComputeHash(userContextIdBinary, array, logonUniqueKey, out keyIndex, out segmentIndex);
         if (Canary15.AreEqual(array3, array2))
         {
             return(new Canary15(userContextIdBinary, array, logonUniqueKey, array2, Canary15.FormatLogData(keyIndex, segmentIndex)));
         }
         ExTraceGlobals.CoreTracer.TraceDebug <string, string>(5L, "testHashBinary={0}!=hashBinary={1}", Canary15.GetHexString(array3), Canary15.GetHexString(array2));
     }
     ExTraceGlobals.CoreTracer.TraceDebug <string, string>(5L, "RestoreCanary failed, logonUniqueKey={0}, canaryString={1}", logonUniqueKey, canaryString);
     return(null);
 }
        static Canary15DataManager()
        {
            Canary15Trace.TraceVersion();
            Canary15Trace.TraceTimeSpan(Canary15DataManager.defaultPeriod, 0, "Canary15DataManager().DefaultPeriod.");
            Canary15DataSegment.SampleUtcNow();
            Canary15DataManager.NextRefreshTime = Canary15DataSegment.UtcNow;
            Canary15DataManager.segments        = new Canary15DataSegment[3];
            for (int i = 0; i < 3; i++)
            {
                Canary15DataManager.segments[i] = Canary15DataSegment.CreateFromADData(i);
            }
            bool flag = Canary15DataManager.segments[0].IsNull || Canary15DataManager.segments[1].IsNull || Canary15DataManager.segments[2].IsNull;

            if (Canary15DataManager.segments[0].IsNull || (Canary15DataManager.segments[1].IsNull && !Canary15DataManager.segments[2].IsNull))
            {
                Canary15DataManager.segments[1].MarkADSegmentForDeletion();
                Canary15DataManager.segments[2].MarkADSegmentForDeletion();
                Canary15DataManager.Create(0);
            }
            if (flag)
            {
                long num   = 36000000000L;
                long ticks = Canary15DataManager.segments[0].Header.ReplicationDuration.Ticks;
                if (ticks == 0L)
                {
                    ticks = Canary15DataSegment.ReplicationDuration.Ticks;
                }
                long num2 = Canary15DataManager.segments[0].Header.StartTime.Ticks - ticks;
                if (num2 > Canary15DataSegment.UtcNow.Ticks)
                {
                    num2 = Canary15DataSegment.UtcNow.Ticks;
                }
                Canary15DataManager.CreateFromLegacyData(2, num2 - num, ticks + num, ticks);
                Canary15DataManager.segments[2].LogToIIS(0);
            }
        }