private void DownloadAndInstall()
        {
            string zipFile;

            // ###########################################
            // ### Download files                      ###
            // ###########################################

            if (Uri.IsWellFormedUriString(dkimSignerAvailable.ZipballUrl, UriKind.RelativeOrAbsolute))
            {
                zipFile = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + ".zip");

                DownloadProgressWindow oDpw = new DownloadProgressWindow(dkimSignerAvailable.ZipballUrl, zipFile);
                try
                {
                    if (oDpw.ShowDialog(this) != DialogResult.OK)
                    {
                        return;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(this, "Couldn't initialize download progress window:\n" + ex.Message, "Error showing download progress", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                finally
                {
                    oDpw.Dispose();
                }
            }
            else
            {
                if (File.Exists(dkimSignerAvailable.ZipballUrl) && Path.GetExtension(dkimSignerAvailable.ZipballUrl) == ".zip")
                {
                    zipFile = dkimSignerAvailable.ZipballUrl;
                }
                else
                {
                    MessageBox.Show(this, "The URL or the path to the ZIP file is invalid. Please try again.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }

            // ###########################################
            // ### Extract files                       ###
            // ###########################################

            string extractDirName = Path.GetDirectoryName(zipFile);

            if (extractDirName == null)
            {
                MessageBox.Show(this, "Could not extract directory name from path: " + zipFile, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            string extractPath = Path.Combine(extractDirName, Path.GetFileNameWithoutExtension(zipFile));

            if (!Directory.Exists(extractPath))
            {
                Directory.CreateDirectory(extractPath);
            }

            try
            {
                ZipFile.ExtractToDirectory(zipFile, extractPath);

                // copy root directory is one directory below extracted zip:
                string[] contents = Directory.GetDirectories(extractPath);
                if (contents.Length == 1)
                {
                    extractPath = Path.Combine(extractPath, contents[0]);
                }
                else
                {
                    MessageBox.Show(this, "Downloaded .zip is invalid. Please try again.", "Invalid download", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "ZIP Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            //now execute the downloaded .exe file

            string exePath = Path.Combine(extractPath, @"Src\Configuration.DkimSigner\bin\Release\Configuration.DkimSigner.exe");

            if (!File.Exists(exePath))
            {
                MessageBox.Show(this, "File not found:\n" + exePath, "Update Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            try
            {
                Process.Start(exePath, "--upgrade-inplace");
                Close();
            }
            catch (Exception ex)
            {
                ShowMessageBox("Updater error", "Couldn't start the process :\n" + ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Esempio n. 2
0
        private void DownloadAndInstall()
        {

            string zipFile;

            // ###########################################
            // ### Download files                      ###
            // ###########################################

            if (Uri.IsWellFormedUriString(dkimSignerAvailable.ZipballUrl, UriKind.RelativeOrAbsolute))
            {
                zipFile = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + ".zip");

                DownloadProgressWindow oDpw = new DownloadProgressWindow(dkimSignerAvailable.ZipballUrl, zipFile);
                try
                {
                    if (oDpw.ShowDialog(this) != DialogResult.OK)
                    {
                        return;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(this, "Couldn't initialize download progress window:\n" + ex.Message, "Error showing download progress", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                finally
                {
                    oDpw.Dispose();
                }

            }
            else
            {
                if (File.Exists(dkimSignerAvailable.ZipballUrl) && Path.GetExtension(dkimSignerAvailable.ZipballUrl) == ".zip")
                {
                    zipFile = dkimSignerAvailable.ZipballUrl;
                }
                else
                {
                    MessageBox.Show(this, "The URL or the path to the ZIP file is invalid. Please try again.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }

            // ###########################################
            // ### Extract files                       ###
            // ###########################################

            string extractDirName = Path.GetDirectoryName(zipFile);

            if (extractDirName == null)
            {
                MessageBox.Show(this, "Could not extract directory name from path: " + zipFile, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            string extractPath = Path.Combine(extractDirName, Path.GetFileNameWithoutExtension(zipFile));

            if (!Directory.Exists(extractPath))
            {
                Directory.CreateDirectory(extractPath);
            }

            try
            {
                ZipFile.ExtractToDirectory(zipFile, extractPath);

                // copy root directory is one directory below extracted zip:
                string[] contents = Directory.GetDirectories(extractPath);
                if (contents.Length == 1)
                {
                    extractPath = Path.Combine(extractPath, contents[0]);
                }
                else
                {
                    MessageBox.Show(this, "Downloaded .zip is invalid. Please try again.", "Invalid download", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "ZIP Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            //now execute the downloaded .exe file

            string exePath = Path.Combine(extractPath, @"Src\Configuration.DkimSigner\bin\Release\Configuration.DkimSigner.exe");
            if (!File.Exists(exePath))
            {
                MessageBox.Show(this, "File not found:\n" + exePath, "Update Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            try
            {
                Process.Start(exePath, "--upgrade-inplace");
                Close();
            }
            catch (Exception ex)
            {
                ShowMessageBox("Updater error", "Couldn't start the process :\n" + ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Esempio n. 3
0
        private void Install()
        {
            progressInstall.Style            = ProgressBarStyle.Continuous;
            btClose.Enabled                  = false;
            gbSelectVersionToInstall.Enabled = false;
            gbInstallStatus.Enabled          = true;
            Refresh();

            // ###########################################
            // ### IS Exchange version supported?      ###
            // ###########################################

            string agentExchangeVersionPath = "";

            foreach (KeyValuePair <string, string> entry in Constants.DkimSignerVersionDirectory)
            {
                if (exchangeVersion.StartsWith(entry.Key))
                {
                    agentExchangeVersionPath = entry.Value;
                    break;
                }
            }

            if (agentExchangeVersionPath == "")
            {
                MessageBox.Show(this, "Your Microsoft Exchange version isn't supported by the DKIM agent: " + exchangeVersion, "Version not supported", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                lbDownloadFiles.Enabled = true;
            }

            // path which is the base for copying the files. Should be the root of the downloaded .zip file.
            string extractPath;

            if (zipUrl != null)
            {
                // ###########################################
                // ### Download files                      ###
                // ###########################################

                string zipFile = "";

                if (Uri.IsWellFormedUriString(zipUrl, UriKind.RelativeOrAbsolute))
                {
                    zipFile = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + ".zip");

                    DownloadProgressWindow oDpw = new DownloadProgressWindow(zipUrl, zipFile);
                    try
                    {
                        if (oDpw.ShowDialog(this) == DialogResult.OK)
                        {
                            lbExtractFiles.Enabled = true;
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(this, "Couldn't initialize download progress window:\n" + ex.Message, "Error showing download progress", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    finally
                    {
                        oDpw.Dispose();
                    }
                }
                else
                {
                    if (File.Exists(zipUrl) && Path.GetExtension(zipUrl) == ".zip")
                    {
                        zipFile = zipUrl;
                        lbExtractFiles.Enabled = true;
                    }
                    else
                    {
                        MessageBox.Show(this, "The URL or the path to the ZIP file is invalid. Please try again.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }

                picDownloadFiles.Image = lbExtractFiles.Enabled ? statusImageList.Images[0] : statusImageList.Images[1];
                progressInstall.Value  = 1;
                Refresh();

                // ###########################################
                // ### Extract files                       ###
                // ###########################################

                string zipDirName = Path.GetDirectoryName(zipFile);
                if (zipDirName == null)
                {
                    MessageBox.Show(this, "Invaild Zip path", "Could not extract directory from zip path: " + zipFile,
                                    MessageBoxButtons.OK, MessageBoxIcon.Error);

                    return;
                }

                extractPath = Path.Combine(zipDirName, Path.GetFileNameWithoutExtension(zipFile));

                if (lbExtractFiles.Enabled)
                {
                    if (!Directory.Exists(extractPath))
                    {
                        Directory.CreateDirectory(extractPath);
                    }

                    try
                    {
                        ZipFile.ExtractToDirectory(zipFile, extractPath);

                        // copy root directory is one directory below extracted zip:
                        string[] contents = Directory.GetDirectories(extractPath);
                        if (contents.Length == 1)
                        {
                            extractPath           = Path.Combine(extractPath, contents[0]);
                            lbStopService.Enabled = true;
                        }
                        else
                        {
                            MessageBox.Show(this, "Downloaded .zip is invalid. Please try again.", "Invalid download", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(this, ex.Message, "ZIP Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }

                picExtractFiles.Image = lbStopService.Enabled ? statusImageList.Images[0] : statusImageList.Images[1];
                progressInstall.Value = 2;
                Refresh();
            }
            else
            {
                // the files are already downloaded and in the same directory as this .exe file

                // the executable is within: \Src\Configuration.DkimSigner\bin\Release so we need to go up a few directories
                DirectoryInfo dir = new FileInfo(Assembly.GetExecutingAssembly().Location).Directory;
                if (dir == null)
                {
                    MessageBox.Show(this, "Could not get directory info for: " + Assembly.GetExecutingAssembly().Location, "Directory error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                string[] expectedDirs = { "Release", "bin", "Configuration.DkimSigner", "Src" };
                bool     sanityFail   = false;

                foreach (string str in expectedDirs)
                {
                    if (dir == null || !dir.Name.Equals(str))
                    {
                        sanityFail = true;
                        break;
                    }
                    dir = dir.Parent;
                }
                if (dir == null)
                {
                    sanityFail = true;
                }

                lbDownloadFiles.Enabled = !sanityFail;
                lbExtractFiles.Enabled  = !sanityFail;
                lbStopService.Enabled   = !sanityFail;

                if (sanityFail)
                {
                    picDownloadFiles.Image = statusImageList.Images[1];
                    picExtractFiles.Image  = statusImageList.Images[1];

                    Refresh();
                    MessageBox.Show(this, "Failed to determine copy root directory.\nThis executable is expected to be in the subpath: \\Src\\Configuration.DkimSigner\\bin\\Release", "ZIP Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                    btClose.Enabled = true;
                    return;
                }

                picDownloadFiles.Image = statusImageList.Images[0];
                picExtractFiles.Image  = statusImageList.Images[0];
                progressInstall.Value  = 2;
                Refresh();

                extractPath = dir.FullName;
            }


            // ###########################################
            // ### Stop Microsoft Transport service    ###
            // ###########################################

            if (lbStopService.Enabled)
            {
                if (transportService.GetStatus() != "Stopped")
                {
                    transportServiceSuccessStatus = "Stopped";
                    transportService.Do(TransportServiceAction.Stop, delegate(string msg)
                    {
                        MessageBox.Show(msg, "Service error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    });
                    lbCopyFiles.Enabled = transportServiceActionCompleted.WaitOne();
                }
                else
                {
                    lbCopyFiles.Enabled = true;
                }
            }

            picStopService.Image  = lbCopyFiles.Enabled ? statusImageList.Images[0] : statusImageList.Images[1];
            progressInstall.Value = 3;
            Refresh();

            // ###########################################
            // ### Copy required files                 ###
            // ###########################################

            if (lbCopyFiles.Enabled)
            {
                List <string> filesToCopy = new List <string>();
                filesToCopy.Add(Path.Combine(extractPath, @"Src\Configuration.DkimSigner\bin\Release"));
                filesToCopy.Add(Path.Combine(extractPath, Path.Combine(@"Src\Exchange.DkimSigner\bin\" + agentExchangeVersionPath)));

                // IF the directory "C:\Program Files\Exchange DkimSigner" doesn't exist, create it
                if (!Directory.Exists(Constants.DkimSignerPath))
                {
                    Directory.CreateDirectory(Constants.DkimSignerPath);
                }

                // Generate list of source files
                string[] sourceFiles = new string[0];
                foreach (string sourcePath in filesToCopy)
                {
                    string[] asTemp = Directory.GetFiles(sourcePath);

                    Array.Resize(ref sourceFiles, sourceFiles.Length + asTemp.Length);
                    Array.Copy(asTemp, 0, sourceFiles, sourceFiles.Length - asTemp.Length, asTemp.Length);
                }

                // Generate list of destinations files
                string[] destinationFiles = new string[sourceFiles.Length];
                for (int i = 0; i < sourceFiles.Length; i++)
                {
                    string sFile = Path.GetFileName(sourceFiles[i]);
                    destinationFiles[i] = Path.Combine(Constants.DkimSignerPath, sFile);
                }

                bool bAnyOperationsAborted;
                bool bReturn = FileOperation.CopyFiles(Handle, sourceFiles, destinationFiles, true, "Copy files", out bAnyOperationsAborted);

                lbInstallAgent.Enabled = bReturn && !bAnyOperationsAborted;
            }

            // register Control Panel Applet
            if (lbInstallAgent.Enabled)
            {
                try
                {
                    CplControl.Register();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(this, "Could not create applet in control panel:\n" + ex.Message + "\nThe installation will be successful anyways but you won't be able to open the DKIM Configurator from the Control Panel", "Error creating applet", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }

                try
                {
                    UninstallerRegistry.Register();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(this, "Could not create uninstall entry in Program and Files:\n" + ex.Message + "\nThe installation will be successful anyways but you won't be able to open the DKIM Configurator from the Control Panel", "Error creating applet", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }

            picCopyFiles.Image    = lbInstallAgent.Enabled ? statusImageList.Images[0] : statusImageList.Images[1];
            progressInstall.Value = 4;
            Refresh();

            // ############################################
            // ### Install DKIM Signer Exchange Agent   ###
            // ############################################

            if (lbInstallAgent.Enabled)
            {
                try
                {
                    // First make sure the following Registry key exists HKLM:\SYSTEM\CurrentControlSet\Services\EventLog\Application\Exchange DKIM
                    if (EventLog.SourceExists(Constants.DkimSignerEventlogSource))
                    {
                        // Make sure we recreate the event log source to fix messageResourceFile from versions previous to 2.0.0
                        RegistryKey key = Registry.LocalMachine.OpenSubKey(Constants.DkimSignerEventlogRegistry, false);
                        if (key == null || key.GetValue("EventMessageFile") == null)
                        {
                            // Delete the event source for the custom event log
                            EventLog.DeleteEventSource(Constants.DkimSignerEventlogSource);

                            // Create a new event source for the custom event log
                            EventSourceCreationData mySourceData = new EventSourceCreationData(Constants.DkimSignerEventlogSource, "Application");
                            mySourceData.MessageResourceFile = @"C:\Windows\Microsoft.NET\Framework\v4.0.30319\EventLogMessages.dll";
                            EventLog.CreateEventSource(mySourceData);
                        }
                    }
                    else
                    {
                        // Create a new event source for the custom event log
                        EventSourceCreationData mySourceData = new EventSourceCreationData(Constants.DkimSignerEventlogSource, "Application");
                        mySourceData.MessageResourceFile = @"C:\Windows\Microsoft.NET\Framework\v4.0.30319\EventLogMessages.dll";
                        EventLog.CreateEventSource(mySourceData);
                    }

                    // Install DKIM Transport Agent in Microsoft Exchange
                    ExchangeServer.InstallDkimTransportAgent();

                    lbStartService.Enabled = true;
                }
                catch (ExchangeServerException ex)
                {
                    MessageBox.Show(this, "Could not install DKIM Agent:\n" + ex.Message, "Error installing agent", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

            picInstallAgent.Image = lbStartService.Enabled ? statusImageList.Images[0] : statusImageList.Images[1];
            progressInstall.Value = 5;
            Refresh();

            // ###########################################
            // ### Start Microsoft Transport service   ###
            // ###########################################

            if (lbStartService.Enabled)
            {
                transportServiceSuccessStatus = "Running";
                transportService.Do(TransportServiceAction.Start, delegate(string msg)
                {
                    MessageBox.Show(msg, "Service error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                });
                picStartService.Image = transportServiceActionCompleted.WaitOne() ? statusImageList.Images[0] : statusImageList.Images[1];
            }
            else
            {
                picStartService.Image = statusImageList.Images[1];
            }

            progressInstall.Value = 6;
            Refresh();

            btClose.Enabled = true;
        }
Esempio n. 4
0
        private void Install()
        {
            progressInstall.Style = ProgressBarStyle.Continuous;
            btClose.Enabled = false;
            gbSelectVersionToInstall.Enabled = false;
            gbInstallStatus.Enabled = true;
            Refresh();

            // ###########################################
            // ### IS Exchange version supported?      ###
            // ###########################################

            string agentExchangeVersionPath = "";

            foreach (KeyValuePair<string, string> entry in Constants.DkimSignerVersionDirectory)
            {
                if (exchangeVersion.StartsWith(entry.Key))
                {
                    agentExchangeVersionPath = entry.Value;
                    break;
                }
            }

            if (agentExchangeVersionPath == "")
            {
                MessageBox.Show(this, "Your Microsoft Exchange version isn't supported by the DKIM agent: " + exchangeVersion, "Version not supported", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                lbDownloadFiles.Enabled = true;
            }

            // path which is the base for copying the files. Should be the root of the downloaded .zip file.
            string extractPath;

            if (zipUrl != null)
            {
                // ###########################################
                // ### Download files                      ###
                // ###########################################

                string zipFile = "";

                if (Uri.IsWellFormedUriString(zipUrl, UriKind.RelativeOrAbsolute))
                {
                    zipFile = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + ".zip");

                    DownloadProgressWindow oDpw = new DownloadProgressWindow(zipUrl, zipFile);
                    try
                    {
                        if (oDpw.ShowDialog(this) == DialogResult.OK)
                        {
                            lbExtractFiles.Enabled = true;
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(this, "Couldn't initialize download progress window:\n" + ex.Message, "Error showing download progress", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    finally
                    {
                        oDpw.Dispose();
                    }

                }
                else
                {
                    if (File.Exists(zipUrl) && Path.GetExtension(zipUrl) == ".zip")
                    {
                        zipFile = zipUrl;
                        lbExtractFiles.Enabled = true;
                    }
                    else
                    {
                        MessageBox.Show(this, "The URL or the path to the ZIP file is invalid. Please try again.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }

                picDownloadFiles.Image = lbExtractFiles.Enabled ? statusImageList.Images[0] : statusImageList.Images[1];
                progressInstall.Value = 1;
                Refresh();

                // ###########################################
                // ### Extract files                       ###
                // ###########################################

                string zipDirName = Path.GetDirectoryName(zipFile);
                if (zipDirName == null)
                {
                    MessageBox.Show(this, "Invaild Zip path", "Could not extract directory from zip path: " + zipFile,
                        MessageBoxButtons.OK, MessageBoxIcon.Error);

                    return;
                }

                extractPath = Path.Combine(zipDirName, Path.GetFileNameWithoutExtension(zipFile));

                if (lbExtractFiles.Enabled)
                {
                    if (!Directory.Exists(extractPath))
                    {
                        Directory.CreateDirectory(extractPath);
                    }

                    try
                    {
                        ZipFile.ExtractToDirectory(zipFile, extractPath);

                        // copy root directory is one directory below extracted zip:
                        string[] contents = Directory.GetDirectories(extractPath);
                        if (contents.Length == 1)
                        {
                            extractPath = Path.Combine(extractPath, contents[0]);
                            lbStopService.Enabled = true;
                        }
                        else
                        {
                            MessageBox.Show(this, "Downloaded .zip is invalid. Please try again.", "Invalid download", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(this, ex.Message, "ZIP Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }

                picExtractFiles.Image = lbStopService.Enabled ? statusImageList.Images[0] : statusImageList.Images[1];
                progressInstall.Value = 2;
                Refresh();
            }
            else
            {
                // the files are already downloaded and in the same directory as this .exe file

                // the executable is within: \Src\Configuration.DkimSigner\bin\Release so we need to go up a few directories
                DirectoryInfo dir = new FileInfo(Assembly.GetExecutingAssembly().Location).Directory;
                if (dir == null)
                {
                    MessageBox.Show(this, "Could not get directory info for: " + Assembly.GetExecutingAssembly().Location, "Directory error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                string[] expectedDirs = { "Release", "bin", "Configuration.DkimSigner", "Src" };
                bool sanityFail = false;

                foreach (string str in expectedDirs)
                {
                    if (dir == null || !dir.Name.Equals(str))
                    {
                        sanityFail = true;
                        break;
                    }
                    dir = dir.Parent;
                }
                if (dir == null)
                    sanityFail = true;

                lbDownloadFiles.Enabled = !sanityFail;
                lbExtractFiles.Enabled = !sanityFail;
                lbStopService.Enabled = !sanityFail;

                if (sanityFail)
                {

                    picDownloadFiles.Image = statusImageList.Images[1];
                    picExtractFiles.Image = statusImageList.Images[1];

                    Refresh();
                    MessageBox.Show(this, "Failed to determine copy root directory.\nThis executable is expected to be in the subpath: \\Src\\Configuration.DkimSigner\\bin\\Release", "ZIP Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                    btClose.Enabled = true;
                    return;
                }

                picDownloadFiles.Image = statusImageList.Images[0];
                picExtractFiles.Image = statusImageList.Images[0];
                progressInstall.Value = 2;
                Refresh();

                extractPath = dir.FullName;

            }


            // ###########################################
            // ### Stop Microsoft Transport service    ###
            // ###########################################

            if (lbStopService.Enabled)
            {
                if (transportService.GetStatus() != "Stopped")
                {
                    transportServiceSuccessStatus = "Stopped";
                    transportService.Do(TransportServiceAction.Stop, delegate(string msg)
                    {
                        MessageBox.Show(msg, "Service error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    });
                    lbCopyFiles.Enabled = transportServiceActionCompleted.WaitOne();
                }
                else
                {
                    lbCopyFiles.Enabled = true;
                }
            }

            picStopService.Image = lbCopyFiles.Enabled ? statusImageList.Images[0] : statusImageList.Images[1];
            progressInstall.Value = 3;
            Refresh();

            // ###########################################
            // ### Copy required files                 ###
            // ###########################################

            if (lbCopyFiles.Enabled)
            {
                List<string> filesToCopy = new List<string>();
                filesToCopy.Add(Path.Combine(extractPath, @"Src\Configuration.DkimSigner\bin\Release"));
                filesToCopy.Add(Path.Combine(extractPath, Path.Combine(@"Src\Exchange.DkimSigner\bin\" + agentExchangeVersionPath)));

                // IF the directory "C:\Program Files\Exchange DkimSigner" doesn't exist, create it 
                if (!Directory.Exists(Constants.DkimSignerPath))
                {
                    Directory.CreateDirectory(Constants.DkimSignerPath);
                }

                // Generate list of source files
                string[] sourceFiles = new string[0];
                foreach (string sourcePath in filesToCopy)
                {
                    string[] asTemp = Directory.GetFiles(sourcePath);

                    Array.Resize(ref sourceFiles, sourceFiles.Length + asTemp.Length);
                    Array.Copy(asTemp, 0, sourceFiles, sourceFiles.Length - asTemp.Length, asTemp.Length);
                }

                // Generate list of destinations files
                string[] destinationFiles = new string[sourceFiles.Length];
                for (int i = 0; i < sourceFiles.Length; i++)
                {
                    string sFile = Path.GetFileName(sourceFiles[i]);
                    destinationFiles[i] = Path.Combine(Constants.DkimSignerPath, sFile);
                }

                bool bAnyOperationsAborted;
                bool bReturn = FileOperation.CopyFiles(Handle, sourceFiles, destinationFiles, true, "Copy files", out bAnyOperationsAborted);

                lbInstallAgent.Enabled = bReturn && !bAnyOperationsAborted;
            }

            // register Control Panel Applet
            if (lbInstallAgent.Enabled)
            {
                try
                {
                    CplControl.Register();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(this, "Could not create applet in control panel:\n" + ex.Message + "\nThe installation will be successful anyways but you won't be able to open the DKIM Configurator from the Control Panel", "Error creating applet", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }

                try
                {
                    UninstallerRegistry.Register();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(this, "Could not create uninstall entry in Program and Files:\n" + ex.Message + "\nThe installation will be successful anyways but you won't be able to open the DKIM Configurator from the Control Panel", "Error creating applet", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }

            }

            picCopyFiles.Image = lbInstallAgent.Enabled ? statusImageList.Images[0] : statusImageList.Images[1];
            progressInstall.Value = 4;
            Refresh();

            // ############################################
            // ### Install DKIM Signer Exchange Agent   ###
            // ############################################

            if (lbInstallAgent.Enabled)
            {
                try
                {
                    // First make sure the following Registry key exists HKLM:\SYSTEM\CurrentControlSet\Services\EventLog\Application\Exchange DKIM
                    if (EventLog.SourceExists(Constants.DkimSignerEventlogSource))
                    {
                        // Make sure we recreate the event log source to fix messageResourceFile from versions previous to 2.0.0
                        RegistryKey key = Registry.LocalMachine.OpenSubKey(Constants.DkimSignerEventlogRegistry, false);
                        if (key == null || key.GetValue("EventMessageFile") == null)
                        {
                            // Delete the event source for the custom event log 
                            EventLog.DeleteEventSource(Constants.DkimSignerEventlogSource);

                            // Create a new event source for the custom event log 
                            EventSourceCreationData mySourceData = new EventSourceCreationData(Constants.DkimSignerEventlogSource, "Application");
                            mySourceData.MessageResourceFile = @"C:\Windows\Microsoft.NET\Framework\v4.0.30319\EventLogMessages.dll";
                            EventLog.CreateEventSource(mySourceData);
                        }
                    }
                    else
                    {
                        // Create a new event source for the custom event log 
                        EventSourceCreationData mySourceData = new EventSourceCreationData(Constants.DkimSignerEventlogSource, "Application");
                        mySourceData.MessageResourceFile = @"C:\Windows\Microsoft.NET\Framework\v4.0.30319\EventLogMessages.dll";
                        EventLog.CreateEventSource(mySourceData);
                    }

                    // Install DKIM Transport Agent in Microsoft Exchange 
                    ExchangeServer.InstallDkimTransportAgent();

                    lbStartService.Enabled = true;
                }
                catch (ExchangeServerException ex)
                {
                    MessageBox.Show(this, "Could not install DKIM Agent:\n" + ex.Message, "Error installing agent", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

            picInstallAgent.Image = lbStartService.Enabled ? statusImageList.Images[0] : statusImageList.Images[1];
            progressInstall.Value = 5;
            Refresh();

            // ###########################################
            // ### Start Microsoft Transport service   ###
            // ###########################################

            if (lbStartService.Enabled)
            {
                transportServiceSuccessStatus = "Running";
                transportService.Do(TransportServiceAction.Start, delegate(string msg)
                {
                    MessageBox.Show(msg, "Service error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                });
                picStartService.Image = transportServiceActionCompleted.WaitOne() ? statusImageList.Images[0] : statusImageList.Images[1];
            }
            else
            {
                picStartService.Image = statusImageList.Images[1];
            }

            progressInstall.Value = 6;
            Refresh();

            btClose.Enabled = true;
        }
Esempio n. 5
0
        private void Install()
        {
            this.btClose.Enabled = false;
            this.gbSelectVersionToInstall.Enabled = false;
            this.gbInstallStatus.Enabled = true;
            this.Refresh();

            // ###########################################
            // ### IS Exchange version supported?      ###
            // ###########################################

            string agentExchangeVersionPath = "";

            foreach (KeyValuePair<string, string> entry in Constants.DKIM_SIGNER_VERSION_DIRECTORY)
            {
                if (exchangeVersion.StartsWith(entry.Key))
                {
                    agentExchangeVersionPath = entry.Value;
                    break;
                }
            }

            if (agentExchangeVersionPath == "")
            {
                MessageBox.Show(this, "Your Microsoft Exchange version isn't supported by the DKIM agent: " + exchangeVersion, "Version not supported", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                this.lbDownloadFiles.Enabled = true;
            }

            // ###########################################
            // ### Download files                      ###
            // ###########################################

            string zipFile = "";

            if (Uri.IsWellFormedUriString(this.zipUrl, UriKind.RelativeOrAbsolute))
            {
                zipFile = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".zip");

                DownloadProgressWindow oDpw = new DownloadProgressWindow(this.zipUrl, zipFile);
                try {
                    if (oDpw.ShowDialog(this) == DialogResult.OK)
                    {
                        this.lbExtractFiles.Enabled = true;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(this, "Couldn't initialize download progress window:\n" + ex.Message, "Error showing download progress", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                    oDpw.Dispose();
                }
                
            }
            else
            {
                if (File.Exists(this.zipUrl) && Path.GetExtension(this.zipUrl) == ".zip")
                {
                    zipFile = this.zipUrl;
                    this.lbExtractFiles.Enabled = true;
                }
                else
                {
                    MessageBox.Show(this, "The URL or the path to the ZIP file is invalid. Please try again.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

            this.picDownloadFiles.Image = this.lbExtractFiles.Enabled ? this.statusImageList.Images[0] : this.statusImageList.Images[1];
            this.Refresh();

            // ###########################################
            // ### Extract files                       ###
            // ###########################################

            string extractPath = Path.Combine(Path.GetDirectoryName(zipFile), Path.GetFileNameWithoutExtension(zipFile));

            if (this.lbExtractFiles.Enabled)
            {
                if (!Directory.Exists(extractPath))
                {
                    Directory.CreateDirectory(extractPath);
                }

                try
                {
                    ZipFile.ExtractToDirectory(zipFile, extractPath);

                    // copy root directory is one directory below extracted zip:
                    string[] contents = Directory.GetDirectories(extractPath);
                    if (contents.Length == 1)
                    {
                        extractPath = Path.Combine(extractPath, contents[0]);
                        this.lbStopService.Enabled = true;
                    }
                    else
                    {
                        MessageBox.Show(this, "Downloaded .zip is invalid. Please try again.", "Invalid download", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }                   
                }
                catch (Exception ex)
                {
                    MessageBox.Show(this, ex.Message, "ZIP Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

            this.picExtractFiles.Image = this.lbStopService.Enabled ? this.statusImageList.Images[0] : this.statusImageList.Images[1];
            this.Refresh();

            // ###########################################
            // ### Stop Microsoft Transport service    ###
            // ###########################################

            if (this.lbStopService.Enabled)
            {
                if (this.transportService.GetStatus() != "Stopped")
                {
                    this.transportServiceSuccessStatus = "Stopped";
                    this.transportService.Do(TransportServiceAction.Stop);
                    this.lbCopyFiles.Enabled = this.transportServiceActionCompleted.WaitOne();
                }
                else
                {
                    this.lbCopyFiles.Enabled = true;
                }
            }

            this.picStopService.Image = this.lbCopyFiles.Enabled ? this.statusImageList.Images[0] : this.statusImageList.Images[1];
            this.Refresh();

            // ###########################################
            // ### Copy required files                 ###
            // ###########################################

            if (this.lbCopyFiles.Enabled)
            {
                List<string> filesToCopy = new List<string>();
                filesToCopy.Add(Path.Combine(extractPath, @"Src\Configuration.DkimSigner\bin\Release"));
                filesToCopy.Add(Path.Combine(extractPath, Path.Combine(@"Src\Exchange.DkimSigner\bin\" + agentExchangeVersionPath)));

                // IF the directory "C:\Program Files\Exchange DkimSigner" doesn't exist, create it 
                if (!Directory.Exists(Constants.DKIM_SIGNER_PATH))
                {
                    Directory.CreateDirectory(Constants.DKIM_SIGNER_PATH);
                }

                // Generate list of source files
                string[] sourceFiles = new string[0];
                foreach (string sourcePath in filesToCopy)
                {
                    string[] asTemp = Directory.GetFiles(sourcePath);

                    Array.Resize(ref sourceFiles, sourceFiles.Length + asTemp.Length);
                    Array.Copy(asTemp, 0, sourceFiles, sourceFiles.Length - asTemp.Length, asTemp.Length);
                }

                // Generate list of destinations files
                string[] destinationFiles = new string[sourceFiles.Length];
                for (int i = 0; i < sourceFiles.Length; i++)
                {
                    string sFile = Path.GetFileName(sourceFiles[i]);
                    destinationFiles[i] = Path.Combine(Constants.DKIM_SIGNER_PATH, sFile);
                }

                bool bAnyOperationsAborted = false;
                bool bReturn = FileOperation.CopyFiles(this.Handle, sourceFiles, destinationFiles, true, "Copy files", out bAnyOperationsAborted);

                this.lbInstallAgent.Enabled = bReturn && !bAnyOperationsAborted;
            }

            this.picCopyFiles.Image = this.lbInstallAgent.Enabled ? this.statusImageList.Images[0] : this.statusImageList.Images[1];
            this.Refresh();

            // ###########################################
            // ### Install DKIM Signer Excange Agent   ###
            // ###########################################

            if (this.lbInstallAgent.Enabled)
            {
                try
                {
                    // First make sure the following Registry key exists HKLM:\SYSTEM\CurrentControlSet\Services\EventLog\Application\Exchange DKIM
                    if (EventLog.SourceExists(Constants.DKIM_SIGNER_EVENTLOG_SOURCE))
                    {
                        // Make sure we recreate the event log source to fix messageResourceFile from versions previous to 2.0.0
                        RegistryKey key = Registry.LocalMachine.OpenSubKey(Constants.DKIM_SIGNER_EVENTLOG_REGISTRY, false);
                        if (key == null || key.GetValue("EventMessageFile") == null)
                        {
                            // Delete the event source for the custom event log 
                            EventLog.DeleteEventSource(Constants.DKIM_SIGNER_EVENTLOG_SOURCE);

                            // Create a new event source for the custom event log 
                            EventSourceCreationData mySourceData = new EventSourceCreationData(Constants.DKIM_SIGNER_EVENTLOG_SOURCE, "Application");
                            mySourceData.MessageResourceFile = @"C:\Windows\Microsoft.NET\Framework\v4.0.30319\EventLogMessages.dll";
                            EventLog.CreateEventSource(mySourceData);
                        }
                    }
                    else
                    {
                        // Create a new event source for the custom event log 
                        EventSourceCreationData mySourceData = new EventSourceCreationData(Constants.DKIM_SIGNER_EVENTLOG_SOURCE, "Application");
                        mySourceData.MessageResourceFile = @"C:\Windows\Microsoft.NET\Framework\v4.0.30319\EventLogMessages.dll";
                        EventLog.CreateEventSource(mySourceData);
                    }

                    // Install DKIM Transport Agent in Microsoft Exchange 
                    ExchangeServer.InstallDkimTransportAgent();

                    this.lbStartService.Enabled = true;
                }
                catch (ExchangeServerException ex)
                {
                    MessageBox.Show(this, "Could not install DKIM Agent:\n" + ex.Message, "Error installing agent", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

            this.picInstallAgent.Image = this.lbStartService.Enabled ? this.statusImageList.Images[0] : this.statusImageList.Images[1];
            this.Refresh();
            
            // ###########################################
            // ### Start Microsoft Transport service   ###
            // ###########################################

            if (this.lbStartService.Enabled)
            {
                this.transportServiceSuccessStatus = "Running";
                this.transportService.Do(TransportServiceAction.Start);
                this.picStartService.Image = this.transportServiceActionCompleted.WaitOne() ? this.statusImageList.Images[0] : this.statusImageList.Images[1];
            }
            else
            {
                this.picStartService.Image = this.statusImageList.Images[1];
            }

            this.Refresh();

            this.btClose.Enabled = true;
        }