public void Register()
 {
     foreach (var descriptor in StorageRegistry.Descriptors)
     {
         FileTransactionEx.Configure(descriptor.Scheme, false);
         System.Net.WebRequest.RegisterPrefix(descriptor.Scheme + ":", new KeeAnywhereWebRequestCreator(this));
     }
 }
Beispiel #2
0
        private static bool SaveConfigFileEx(AppConfigEx tConfig,
                                             string strFilePath, bool bRemoveConfigPref)
        {
            tConfig.OnSavePre();

            // Temporarily remove user file preference (restore after saving)
            bool bConfigPref = tConfig.Meta.PreferUserConfiguration;

            if (bRemoveConfigPref)
            {
                tConfig.Meta.PreferUserConfiguration = false;
            }

            bool bResult = true;

            try
            {
                Debug.Assert(!string.IsNullOrEmpty(strFilePath));
                IOConnectionInfo iocPath = IOConnectionInfo.FromPath(strFilePath);

                using (FileTransactionEx ft = new FileTransactionEx(iocPath,
                                                                    Program.Config.Application.UseTransactedConfigWrites))
                {
                    using (Stream s = ft.OpenWrite())
                    {
                        using (XmlWriter xw = XmlUtilEx.CreateXmlWriter(s))
                        {
                            XmlSerializerEx xs = new XmlSerializerEx(typeof(AppConfigEx));
                            xs.Serialize(xw, tConfig);
                        }
                    }

                    ft.CommitWrite();
                }
            }
            catch (Exception) { Debug.Assert(false); bResult = false; }

            if (bRemoveConfigPref)
            {
                tConfig.Meta.PreferUserConfiguration = bConfigPref;
            }

            AssertConfigPref(tConfig);

            tConfig.OnSavePost();
            return(bResult);
        }
        public void Register()
        {
            try
            {
                foreach (string strPrefix in m_vSupportedSchemes)
                {
                    WebRequest.RegisterPrefix(strPrefix, this);
                }

                // scp not support operation move and delete. Then sync via scp, do without transaction (direct write to target remote file)
                FileTransactionEx.Configure("scp", false);
            } catch (Exception e)
            {
                MessageBox.Show(e.Message, "Error on Register", MessageBoxButtons.OK, MessageBoxIcon.Error);
                throw;
            }
        }
		/// <summary>
		/// Save the currently opened database. The file is written to the location
		/// it has been opened from.
		/// </summary>
		/// <param name="slLogger">Logger that recieves status information.</param>
		public void Save(IStatusLogger slLogger)
		{
			Debug.Assert(ValidateUuidUniqueness());

			// bool bMadeUnhidden = UrlUtil.UnhideFile(m_ioSource.Path);
			// Stream s = IOConnection.OpenWrite(m_ioSource);

			FileTransactionEx ft = new FileTransactionEx(m_ioSource, m_bUseFileTransactions);
			Stream s = ft.OpenWrite();

			Kdb4File kdb = new Kdb4File(this);
			kdb.Save(s, null, Kdb4Format.Default, slLogger);

			ft.CommitWrite();

			// if(bMadeUnhidden) UrlUtil.HideFile(m_ioSource.Path, true); // Hide again

			m_pbHashOfLastIO = kdb.HashOfFileOnDisk;
			m_pbHashOfFileOnDisk = kdb.HashOfFileOnDisk;
			Debug.Assert(m_pbHashOfFileOnDisk != null);

			m_bModified = false;
		}
Beispiel #5
0
 public BuiltInFileTransaction(IOConnectionInfo ioc, bool useFileTransaction, BuiltInFileStorage fileStorage)
 {
     _ioc         = ioc;
     _fileStorage = fileStorage;
     _transaction = new FileTransactionEx(ioc, useFileTransaction);
 }
Beispiel #6
0
        private bool EncryptAndSave(byte[] secret)
        {
            //generate a random challenge for use next time
            byte[] challenge = GenerateChallenge();

            //generate the expected HMAC-SHA1 response for the challenge based on the secret
            byte[] resp = GenerateResponse(challenge, secret);

            //use the response to encrypt the secret
            SHA256 sha = SHA256Managed.Create();

            byte[] key        = sha.ComputeHash(resp); // get a 256 bit key from the 160 bit hmac response
            byte[] secretHash = sha.ComputeHash(secret);

            AesManaged aes = new AesManaged();

            aes.KeySize = key.Length * sizeof(byte) * 8; //pedantic, but foolproof
            aes.Key     = key;
            aes.GenerateIV();
            aes.Padding = PaddingMode.PKCS7;
            byte[] iv = aes.IV;

            byte[]           encrypted;
            ICryptoTransform enc = aes.CreateEncryptor();

            using (MemoryStream msEncrypt = new MemoryStream())
            {
                using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, enc, CryptoStreamMode.Write))
                {
                    csEncrypt.Write(secret, 0, secret.Length);
                    csEncrypt.FlushFinalBlock();

                    encrypted = msEncrypt.ToArray();
                    csEncrypt.Close();
                    csEncrypt.Clear();
                }
                msEncrypt.Close();
            }

            sha.Clear();
            aes.Clear();

            Stream s = null;

            try
            {
                FileTransactionEx ft = new FileTransactionEx(mInfo,
                                                             false);
                s = ft.OpenWrite();

                XmlWriterSettings settings = new XmlWriterSettings();
                settings.CloseOutput         = true;
                settings.Indent              = true;
                settings.IndentChars         = "\t";
                settings.NewLineOnAttributes = true;

                XmlWriter xml = XmlWriter.Create(s, settings);
                xml.WriteStartDocument();
                xml.WriteStartElement("data");

                xml.WriteStartElement("aes");
                xml.WriteElementString("encrypted", Convert.ToBase64String(encrypted));
                xml.WriteElementString("iv", Convert.ToBase64String(iv));
                xml.WriteEndElement();

                xml.WriteElementString("challenge", Convert.ToBase64String(challenge));
                xml.WriteElementString("verification", Convert.ToBase64String(secretHash));
                xml.WriteElementString("lt64", LT64.ToString());

                xml.WriteEndElement();
                xml.WriteEndDocument();
                xml.Close();

                ft.CommitWrite();
            }
            catch (Exception)
            {
                MessageService.ShowWarning(String.Format("Error: unable to write to file {0}", mInfo.Path));
                return(false);
            }
            finally
            {
                s.Close();
            }

            return(true);
        }
Beispiel #7
0
        private static bool SaveConfigFileEx(AppConfigEx tConfig,
                                             string strFilePath, bool bRemoveConfigPref)
        {
            tConfig.OnSavePre();

            XmlSerializerEx xmlSerial = new XmlSerializerEx(typeof(AppConfigEx));
            bool            bResult   = true;

            // FileStream fs = null;
            IOConnectionInfo  iocPath = IOConnectionInfo.FromPath(strFilePath);
            FileTransactionEx fts     = new FileTransactionEx(iocPath, true);
            Stream            fs      = null;

            // Temporarily remove user file preference (restore after saving)
            bool bConfigPref = tConfig.Meta.PreferUserConfiguration;

            if (bRemoveConfigPref)
            {
                tConfig.Meta.PreferUserConfiguration = false;
            }

            XmlWriterSettings xws = new XmlWriterSettings();

            xws.Encoding    = StrUtil.Utf8;
            xws.Indent      = true;
            xws.IndentChars = "\t";

            try
            {
                // fs = new FileStream(strFilePath, FileMode.Create,
                //	FileAccess.Write, FileShare.None);
                fs = fts.OpenWrite();
                if (fs == null)
                {
                    throw new InvalidOperationException();
                }

                XmlWriter xw = XmlWriter.Create(fs, xws);
                xmlSerial.Serialize(xw, tConfig);
                xw.Close();
            }
            catch (Exception) { bResult = false; }            // Do not assert

            if (fs != null)
            {
                fs.Close(); fs = null;
            }
            if (bResult)
            {
                try { fts.CommitWrite(); }
                catch (Exception) { Debug.Assert(false); }
            }

            if (bRemoveConfigPref)
            {
                tConfig.Meta.PreferUserConfiguration = bConfigPref;
            }

            tConfig.OnSavePost();
            return(bResult);
        }
Beispiel #8
0
 public TestFileTransaction(IOConnectionInfo ioc, bool useFileTransaction, bool offline)
 {
     _offline     = offline;
     _transaction = new FileTransactionEx(ioc, useFileTransaction);
 }