Exemple #1
0
        private void Decrypt_Callback(GpgInterfaceResult result, string tmpFile, string path)
        {
            if (result.Status == GpgInterfaceStatus.Success)
            {
                List<GpgApi.KeyId> recipients = new List<KeyId>() { };
                foreach (var line in listBox1.Items)
                {
                    GpgListSecretKeys publicKeys = new GpgListSecretKeys();
                    publicKeys.Execute();
                    foreach (Key key in publicKeys.Keys)
                    {
                        if (key.UserInfos[0].Email == line.ToString())
                        {
                            recipients.Add(key.Id);
                        }
                    }
                }

                string tmpFile2 = Path.GetTempFileName();
                GpgEncrypt encrypt = new GpgEncrypt(tmpFile, tmpFile2, false, false, null, recipients, GpgApi.CipherAlgorithm.None);
                GpgInterfaceResult enc_result = encrypt.Execute();
                Encrypt_Callback(enc_result, tmpFile, tmpFile2, path);
            }
            else
            {
                // shit happened
            }
        }
Exemple #2
0
 /// <summary>
 /// Callback from Decrypt_Callback, ensures the tmp files are deleted and the file get's the correct name
 /// </summary>
 /// <param name="result"></param>
 /// <param name="tmpFile"></param>
 /// <param name="tmpFile2"></param>
 /// <param name="path"></param>
 public void EncryptCallback(GpgInterfaceResult result, string tmpFile, string tmpFile2, string path)
 {
     if (result.Status == GpgInterfaceStatus.Success)
     {
         File.Delete(tmpFile);
         File.Delete(path);
         File.Move(tmpFile2, path);
     }
 }
Exemple #3
0
        // ----------------------------------------------------------------------------------------

        // internal AND protected
        internal void EmitEvent(GpgInterfaceResult result)
        {
            Log(result.Status.ToString());

            if (GpgInterfaceEvent == null)
            {
                return;
            }

            if (SynchronizationContext == null)
            {
                GpgInterfaceEvent(this, result);
            }
            else
            {
                SynchronizationContext.Send(delegate { GpgInterfaceEvent(this, result); }, null);
            }
        }
Exemple #4
0
        public void ExecuteAsync(Action <GpgInterfaceResult> completed)
        {
            Thread thread = new Thread(delegate()
            {
                GpgInterfaceResult result = Execute();

                if (completed == null)
                {
                    return;
                }

                if (SynchronizationContext == null)
                {
                    completed(result);
                }
                else
                {
                    SynchronizationContext.Send(delegate { completed(result); }, null);
                }
            });

            thread.Name = ToString();
            thread.Start();
        }
Exemple #5
0
        public GpgInterfaceResult Execute()
        {
            GpgInterfaceResult result = null;

            try
            {
                lock (_alreadyUsedLock)
                {
                    if (_alreadyUsed)
                    {
                        throw new GpgInterfaceAlreadyUsed();
                    }
                    _alreadyUsed = true;
                }

                if (!File.Exists(GpgInterface.ExePath))
                {
                    throw new FileNotFoundException(null, GpgInterface.ExePath);
                }

                _outputEventWait  = new ManualResetEvent(false);
                _abortedEventWait = new ManualResetEvent(false);
                _exitedEventWait  = new ManualResetEvent(false);

                Log("Execute " + this);

                result = GpgInterfaceResult.Started;
                EmitEvent(result);

                result = BeforeStartProcess();
                if (result.Status != GpgInterfaceStatus.Success)
                {
                    EmitEvent(result);
                    return(result);
                }

                _process         = GetProcess(Arguments());
                _process.Exited += delegate(Object sender, EventArgs args)
                {
                    _processExited = true;
                    _exitedEventWait.Set();
                };
                _process.EnableRaisingEvents = true;

                _process.Start();
                _process.BeginErrorReadLine();

                _outputThread = new Thread(OutputReader)
                {
                    Name = ToString() + " - OutputReader"
                };
                _outputThread.Start();

                while (WaitHandle.WaitAny(new WaitHandle[] { _outputEventWait, _abortedEventWait, _exitedEventWait }) != -1)
                {
                    if (_aborted)
                    {
                        break;
                    }

                    if (_processExited)
                    {
                        if (_outputThread != null)
                        {
                            _outputThread.Join();
                        }
                        _outputThread = null;
                    }

                    _outputEventWait.Reset();

                    GpgOutput output;
                    while (!_aborted && _output.TryDequeue(out output))
                    {
                        Log(output.Str);
                        result = ProcessLine(output.Str);

                        if (result.Status == GpgInterfaceStatus.Success)
                        {
                            continue;
                        }

                        EmitEvent(result);

                        if (result.Status == GpgInterfaceStatus.Error)
                        {
                            _aborted = true;
                            break;
                        }
                    }

                    if (_processExited && _outputThread == null)
                    {
                        break;
                    }
                }

                if (!_aborted)
                {
                    result = GpgInterfaceResult.Success;
                    EmitEvent(result);
                }
                else if (_aborted && result.Status != GpgInterfaceStatus.Error)
                {
                    result = GpgInterfaceResult.UserAbort;
                    EmitEvent(result);
                }

                Log("Exit");
            }
            catch (Exception e)
            {
                result = new GpgInterfaceResult(GpgInterfaceStatus.Error, GpgInterfaceMessage.None, e);
                EmitEvent(result);
            }
            finally
            {
                CleanUp();
            }

            return(result);
        }
Exemple #6
0
 /// <summary>
 ///     Callback for the decrypt thread
 /// </summary>
 /// <param name="result"></param>
 /// <param name="clear"></param>
 private void DecryptCallback(GpgInterfaceResult result, bool clear)
 {
     if (result.Status == GpgInterfaceStatus.Success)
     {
         txtPassDetail.Text = File.ReadAllText(this.tmpfile);
         File.Delete(this.tmpfile);
         // copy to clipboard
         if (txtPassDetail.Text != "")
         {
             Clipboard.SetText(new string(txtPassDetail.Text.TakeWhile(c => c != '\n').ToArray()));
             if (clear)
             {
                 // set progressbar as notification
                 statusPB.Maximum = 45;
                 statusPB.Value = 0;
                 statusPB.Step = 1;
                 statusPB.Visible = true;
                 statusTxt.Text = Strings.Statusbar_countdown + @" ";
                 //Create the timer
                 clipboardTimer = new Timer(ClearClipboard, null, 0, 1000);
             }
         }
     }
     else
     {
         txtPassDetail.Text = Strings.Error_weird_shit_happened;
     }
 }
Exemple #7
0
 /// <summary>
 ///     Callback for the encrypt thread
 /// </summary>
 /// <param name="result"></param>
 /// <param name="tmpFile"></param>
 /// <param name="tmpFile2"></param>
 /// <param name="path"></param>
 public void EncryptCallback(GpgInterfaceResult result, string tmpFile, string tmpFile2, string path)
 {
     if (result.Status == GpgInterfaceStatus.Success)
     {
         File.Delete(tmpFile);
         File.Delete(path);
         File.Move(tmpFile2, path);
         // add to git
         using (var repo = new Repository(Cfg["PassDirectory"]))
         {
             // Stage the file
             repo.Stage(path);
             // Commit
             repo.Commit("password changes", new Signature("pass4win", "pass4win", DateTimeOffset.Now),
                 new Signature("pass4win", "pass4win", DateTimeOffset.Now));
             if (Cfg["UseGitRemote"] == true && this.gitRepoOffline == false)
             {
                 toolStripOffline.Visible = false;
                 var remote = repo.Network.Remotes["origin"];
                 var options = new PushOptions
                 {
                     CredentialsProvider = (url, user, cred) => new UsernamePasswordCredentials
                     {
                         Username = Cfg["GitUser"],
                         Password = DecryptConfig(Cfg["GitPass"], "pass4win")
                     }
                 };
                 var pushRefSpec = @"refs/heads/master";
                 repo.Network.Push(remote, pushRefSpec, options);
             }
         }
     }
     else
     {
         MessageBox.Show(Strings.Error_weird_shit_happened_encryption, Strings.Error, MessageBoxButtons.OK,
             MessageBoxIcon.Error);
     }
 }
Exemple #8
0
        // ----------------------------------------------------------------------------------------
        // internal AND protected
        internal void EmitEvent(GpgInterfaceResult result)
        {
            Log(result.Status.ToString());

            if (GpgInterfaceEvent == null)
                return;

            if (SynchronizationContext == null)
                GpgInterfaceEvent(this, result);
            else
                SynchronizationContext.Send(delegate { GpgInterfaceEvent(this, result); }, null);
        }
Exemple #9
0
        public GpgInterfaceResult Execute()
        {
            GpgInterfaceResult result = null;

            try
            {
                lock (_alreadyUsedLock)
                {
                    if (_alreadyUsed)
                        throw new GpgInterfaceAlreadyUsed();
                    _alreadyUsed = true;
                }

                if (!File.Exists(GpgInterface.ExePath))
                    throw new FileNotFoundException(null, GpgInterface.ExePath);

                _outputEventWait = new ManualResetEvent(false);
                _abortedEventWait = new ManualResetEvent(false);
                _exitedEventWait = new ManualResetEvent(false);

                Log("Execute " + this);

                result = GpgInterfaceResult.Started;
                EmitEvent(result);

                result = BeforeStartProcess();
                if (result.Status != GpgInterfaceStatus.Success)
                {
                    EmitEvent(result);
                    return result;
                }

                _process = GetProcess(Arguments());
                _process.Exited += delegate(Object sender, EventArgs args)
                {
                    _processExited = true;
                    _exitedEventWait.Set();
                };
                _process.EnableRaisingEvents = true;

                _process.Start();
                _process.BeginErrorReadLine();

                _outputThread = new Thread(OutputReader) { Name = ToString() + " - OutputReader" };
                _outputThread.Start();

                while (WaitHandle.WaitAny(new WaitHandle[] { _outputEventWait, _abortedEventWait, _exitedEventWait }) != -1)
                {
                    if (_aborted)
                        break;

                    if (_processExited)
                    {
                        if (_outputThread != null)
                            _outputThread.Join();
                        _outputThread = null;
                    }

                    _outputEventWait.Reset();

                    GpgOutput output;
                    while (!_aborted && _output.TryDequeue(out output))
                    {
                        Log(output.Str);
                        result = ProcessLine(output.Str);

                        if (result.Status == GpgInterfaceStatus.Success)
                            continue;

                        EmitEvent(result);

                        if (result.Status == GpgInterfaceStatus.Error)
                        {
                            _aborted = true;
                            break;
                        }
                    }

                    if (_processExited && _outputThread == null)
                        break;
                }

                if (!_aborted)
                {
                    result = GpgInterfaceResult.Success;
                    EmitEvent(result);
                }
                else if (_aborted && result.Status != GpgInterfaceStatus.Error)
                {
                    result = GpgInterfaceResult.UserAbort;
                    EmitEvent(result);
                }

                Log("Exit");
            }
            catch (Exception e)
            {
                result = new GpgInterfaceResult(GpgInterfaceStatus.Error, GpgInterfaceMessage.None, e);
                EmitEvent(result);
            }
            finally
            {
                CleanUp();
            }

            return result;
        }
Exemple #10
0
 // Callback for the decrypt thread
 private void Decrypt_Callback(GpgInterfaceResult result, bool clear)
 {
     if (result.Status == GpgInterfaceStatus.Success)
     {
         txtPassDetail.Text = File.ReadAllText(this.tmpfile);
         File.Delete(tmpfile);
         // copy to clipboard
         if (txtPassDetail.Text != "")
         {
             Clipboard.SetText(new string(txtPassDetail.Text.TakeWhile(c => c != '\n').ToArray()));
             if (clear)
             {
                 // set progressbar as notification
                 statusPB.Maximum = 45;
                 statusPB.Value = 0;
                 statusPB.Step = 1;
                 statusPB.Visible = true;
                 statusTxt.Text = "Countdown to clearing clipboard  ";
                 //Create the timer
                 _timer = new System.Threading.Timer(ClearClipboard, null, 0, 1000);
             }
         }
     }
     else
     {
         txtPassDetail.Text = "Something went wrong.....";
     }
 }
Exemple #11
0
 // Callback for the encrypt thread
 public void Encrypt_Callback(GpgInterfaceResult result, string tmpFile, string tmpFile2, string path)
 {
     if (result.Status == GpgInterfaceStatus.Success)
     {
         File.Delete(tmpFile);
         File.Delete(path);
         File.Move(tmpFile2, path);
         // add to git
         using (var repo = new Repository(Properties.Settings.Default.PassDirectory))
         {
             // Stage the file
             repo.Stage(path);
             // Commit
             repo.Commit("password changes", new Signature("pass4win", "pass4win", System.DateTimeOffset.Now), new Signature("pass4win", "pass4win", System.DateTimeOffset.Now));
             var remote = repo.Network.Remotes["origin"];
             var options = new PushOptions();
             options.CredentialsProvider = (_url, _user, _cred) => new UsernamePasswordCredentials
             {
                 Username = GitUsername,
                 Password = GitPassword
             };
             var pushRefSpec = @"refs/heads/master";
             repo.Network.Push(remote, pushRefSpec, options, new Signature("pass4win", "*****@*****.**", DateTimeOffset.Now),
                 "pushed changes");
         }
     }
     else
     {
         MessageBox.Show("You shouldn't see this.... Awkward right... Encryption failed", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }
Exemple #12
0
        /// <summary>
        ///     Callback for the encrypt thread
        /// </summary>
        /// <param name="result"></param>
        /// <param name="tmpFile"></param>
        /// <param name="tmpFile2"></param>
        /// <param name="path"></param>
        public void EncryptCallback(GpgInterfaceResult result, string tmpFile, string tmpFile2, string path)
        {
            if (result.Status == GpgInterfaceStatus.Success)
            {
                File.Delete(tmpFile);
                File.Delete(path);
                File.Move(tmpFile2, path);
                // add to git
                if (!Program.NoGit)
                {
                    if (!GitRepo.Commit(path))
                    {
                        MessageBox.Show(
                            Strings.FrmMain_EncryptCallback_Commit_failed_,
                            Strings.Error,
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
                        return;
                    }
                }

                // Check if we want to sync with remote
                if (this.gitRepoOffline)
                {
                    return;
                }

                // sync with remote
                if (!Program.NoGit)
                {
                    if (!GitRepo.Push(_config["GitUser"], DecryptConfig(_config["GitPass"], "pass4win")))
                    {
                        MessageBox.Show(
                            Strings.FrmMain_EncryptCallback_Push_to_remote_repo_failed_,
                            Strings.Error,
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
                    }
                }
                log.Debug("Encryption finished");
            }
        }