Пример #1
0
        private void addToolStripMenuItem_Click(object sender, EventArgs e)
        {
            KeySelect newKeySelect = new KeySelect();
            if (newKeySelect.ShowDialog() == DialogResult.OK)
            {
                if (listBox1.Items[0].ToString() == "There are no specific keys set")
                    listBox1.Items.Clear();
                listBox1.Items.Add(newKeySelect.gpgkey);
                string tmpFile = Path.GetDirectoryName(Properties.Settings.Default.PassDirectory) + "\\" + treeView1.SelectedNode.FullPath + "\\.gpg-id";
                using (StreamWriter w = new StreamWriter(tmpFile))
                {
                    foreach(var line in listBox1.Items){
                        w.WriteLine(line.ToString());
                    }
                }

                DirectoryInfo path = new DirectoryInfo(Path.GetDirectoryName(Properties.Settings.Default.PassDirectory) + "\\" + treeView1.SelectedNode.FullPath);

                foreach (var ffile in path.GetFiles())
                {
                    if (!ffile.Name.StartsWith("."))
                        recrypt(ffile.FullName);
                }

                ScanDirectory(path);
            }
        }
Пример #2
0
        /// <summary>
        /// Adds a key to a selected directory
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void addToolStripMenuItem_Click(object sender, EventArgs e)
        {
            KeySelect newKeySelect = new KeySelect();

            if (newKeySelect.ShowDialog() == DialogResult.OK)
            {
                if (listBox1.Items[0].ToString() == Strings.Error_keys_set)
                {
                    listBox1.Items.Clear();
                }
                listBox1.Items.Add(newKeySelect.gpgkey);
                string tmpFile = Path.GetDirectoryName(frmMain.cfg["PassDirectory"]) + "\\" + treeView1.SelectedNode.FullPath + "\\.gpg-id";
                using (StreamWriter w = new StreamWriter(tmpFile))
                {
                    foreach (var line in listBox1.Items)
                    {
                        w.WriteLine(line.ToString());
                    }
                }

                DirectoryInfo path = new DirectoryInfo(Path.GetDirectoryName(frmMain.cfg["PassDirectory"]) + "\\" + treeView1.SelectedNode.FullPath);

                foreach (var ffile in path.GetFiles())
                {
                    if (!ffile.Name.StartsWith("."))
                    {
                        recrypt(ffile.FullName);
                    }
                }

                ScanDirectory(path);
            }
            newKeySelect.Close();
        }
Пример #3
0
        /// <summary>
        /// Adds a key to a selected directory
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AddToolStripMenuItemClick(object sender, EventArgs e)
        {
            KeySelect newKeySelect = new KeySelect();
            if (newKeySelect.ShowDialog() == DialogResult.OK)
            {
                if (listBox1.Items[0].ToString() == Strings.Error_keys_set)
                {
                    listBox1.Items.Clear();
                }
                listBox1.Items.Add(newKeySelect.Gpgkey);
                string tmpFile = Path.GetDirectoryName(FrmMain.Cfg["PassDirectory"]) + "\\" + treeView1.SelectedNode.FullPath + "\\.gpg-id";
                using (StreamWriter w = new StreamWriter(tmpFile))
                {
                    foreach (var line in listBox1.Items)
                    {
                        w.WriteLine(line.ToString());
                    }
                }

                DirectoryInfo path = new DirectoryInfo(Path.GetDirectoryName(FrmMain.Cfg["PassDirectory"]) + "\\" + treeView1.SelectedNode.FullPath);

                foreach (var ffile in path.GetFiles())
                {
                    if (!ffile.Name.StartsWith("."))
                        Recrypt(ffile.FullName);
                }

                ScanDirectory(path);
            }
            newKeySelect.Close();
        }
Пример #4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="FrmKeyManager"/> class.
        /// </summary>
        public FrmKeyManager(FileSystemInterface fileSystemInterface, ConfigHandling config, KeySelect keySelect)
        {
            _fileSystemInterface = fileSystemInterface;
            _config    = config;
            _keySelect = keySelect;
            InitializeComponent();
            GpgInterface.ExePath = _config["GPGEXE"];

            ListDirectory(treeView1);
        }
Пример #5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="FrmKeyManager"/> class.
        /// </summary>
        public FrmKeyManager(FileSystemInterface fileSystemInterface, ConfigHandling config, KeySelect keySelect)
        {
            _fileSystemInterface = fileSystemInterface;
            _config = config;
            _keySelect = keySelect;
            InitializeComponent();
            GpgInterface.ExePath = _config["GPGEXE"];

            ListDirectory(treeView1);
        }
Пример #6
0
        /// <summary>
        ///     Inits the repo, gpg etc
        /// </summary>
        public FrmMain(FileSystemInterface fileSystemInterface, KeySelect keySelect, ConfigHandling config)
        {
            log.Debug("Init variables");
            fsi        = fileSystemInterface;
            _keySelect = keySelect;
            _config    = config;
            InitializeComponent();
            toolStripStatusLabel1.Text = "";

            log.Debug("init Bugsnag");
            var bugsnag = new BaseClient("23814316a6ecfe8ff344b6a467f07171");

            this.enableTray = false;

            // Getting actual version
            log.Debug("Getting program version");
            var assembly = Assembly.GetExecutingAssembly();
            var fvi      = FileVersionInfo.GetVersionInfo(assembly.Location);
            var version  = fvi.FileVersion;

            _config["version"] = version.Remove(5);
            Text = @"Pass4Win " + Strings.Version + @" " + _config["version"];
            // check config input
            log.Debug("Checking config validity");
            try
            {
                bool ChkBool = Directory.Exists(_config["PassDirectory"]);
                if (!ChkBool)
                {
                    throw new Exception("Pass directory fail");
                }
                ChkBool = File.Exists(_config["GPGEXE"]);
                if (!ChkBool)
                {
                    throw new Exception("GPG location fail");
                }
                if (_config["UseGitRemote"] || _config["ExternalGit"])
                {
                    // nobody cares, we want the exception and it's auto generated due to a null value or not a boolean
                }
            }
            catch
            {
                // no matter if it's a first run or a corrupt entry, let them fix it.
                log.Debug("Config corrupt or first run");
                _config["FirstRun"] = true;
                Program.Scope.Resolve <FrmConfig>().ShowDialog();
            }

            // checking for update in the background
            log.Debug("Checking online for latest release");
            LatestPass4WinRelease();

            // making new git object
            if (_config["ExternalGit"])
            {
                log.Debug("Using an external git executable");
                GitRepo = new GitHandling(_config["PassDirectory"], _config["GitRemote"], _config["ExternalGitLocation"]);
            }
            else
            {
                log.Debug("Using the internal git executable");
                GitRepo = new GitHandling(_config["PassDirectory"], _config["GitRemote"]);
            }

            //checking git status
            if (_config["UseGitRemote"] == true || _config["ExternalGit"])
            {
                if (GitRepo.ConnectToRepo() || (_config["ExternalGit"]))
                {
                    log.Debug("Remote Git is valid and active");
                    this.gitRepoOffline      = false;
                    toolStripOffline.Visible = false;
                    if (!GitRepo.Fetch(_config["GitUser"], DecryptConfig(_config["GitPass"], "pass4win")))
                    {
                        // Something failed which shouldn't fail, rechecking online status
                        CheckOnline(false);
                    }
                }
                // checking if we can clone, otherwise make a new one
                else if (!GitRepo.GitClone(_config["GitUser"], DecryptConfig(_config["GitPass"], "pass4win")))
                {
                    log.Debug("Making a new git repo");
                    Repository.Init(_config["PassDirectory"], false);
                    GitRepo.ConnectToRepo();
                    toolStripOffline.Visible = true;
                }
            }
            // no connection, no repo. So make a new one
            else if (!GitHandling.IsValid(_config["PassDirectory"]))
            {
                log.Debug("Making a new git repo");
                Repository.Init(_config["PassDirectory"], false);
                GitRepo.ConnectToRepo();
                toolStripOffline.Visible = true;
            }

            // Init GPG if needed
            string gpgfile = _config["PassDirectory"];

            gpgfile += "\\.gpg-id";
            // Check if we need to init the directory
            if (!File.Exists(gpgfile))
            {
                log.Debug("Creating .gpg-id");
                // ReSharper disable once AssignNullToNotNullAttribute
                Directory.CreateDirectory(Path.GetDirectoryName(gpgfile));
                if (_keySelect.ShowDialog() == DialogResult.OK)
                {
                    using (var w = new StreamWriter(gpgfile))
                    {
                        w.Write(_keySelect.Gpgkey);
                    }

                    if (!GitRepo.Commit(gpgfile))
                    {
                        MessageBox.Show(
                            Strings.FrmMain_EncryptCallback_Commit_failed_,
                            Strings.Error,
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
                        return;
                    }
                }
                else
                {
                    _keySelect.Close();
                    MessageBox.Show(Strings.Error_nokey, Strings.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    Environment.Exit(1);
                }
            }
            // Setting the exe location for the GPG Dll
            GpgInterface.ExePath = _config["GPGEXE"];

            // Fill tree
            CreateNodes();

            this.enableTray = true;
        }
Пример #7
0
        /// <summary>
        ///     Inits the repo, gpg etc
        /// </summary>
        public FrmMain()
        {
            InitializeComponent();
            toolStripStatusLabel1.Text = "";

            // ReSharper disable once UnusedVariable
            var bugsnag = new BaseClient("23814316a6ecfe8ff344b6a467f07171");

            this.enableTray = false;

            // Getting actual version
            var assembly = Assembly.GetExecutingAssembly();
            var fvi = FileVersionInfo.GetVersionInfo(assembly.Location);
            var version = fvi.FileVersion;
            Cfg["version"] = version.Remove(5, 2);

            Text = @"Pass4Win " + Strings.Version + @" " + Cfg["version"];


            // checking for update this an async operation
#pragma warning disable 4014
            LatestPass4WinRelease();
#pragma warning restore 4014

            // Do we have a valid password store and settings
            try
            {
                if (Cfg["PassDirectory"] == "")
                {
                    // this will fail, I know ugly hack
                }
            }
            catch (Exception)
            {
                Cfg["FirstRun"] = true;
                var config = new FrmConfig();
                config.ShowDialog();
            }
            //checking git status
            if (!IsValid(Cfg["PassDirectory"]))
            {
                // Remote or generate a new one
                if (Cfg["UseGitRemote"] == true)
                {
                    // check if server is alive
                    if (IsGitAlive(Cfg["UseGitRemote"]) || IsHttpsAlive(Cfg["UseGitRemote"]))
                    {
                        // clone the repo and catch any error
                        try
                        {
                            Clone(Cfg["GitRemote"], Cfg["PassDirectory"],
                                new CloneOptions
                                {
                                    CredentialsProvider = (url, user, cred) => new UsernamePasswordCredentials
                                    {
                                        Username = Cfg["GitUser"],
                                        Password = DecryptConfig(Cfg["GitPass"], "pass4win")
                                    }
                                });
                        }
                        catch (Exception)
                        {
                            MessageBox.Show(Strings.Error_connection, Strings.Error, MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                            this.toolStripOffline.Visible = true;
                        }
                    }
                    else
                    {
                        MessageBox.Show(Strings.Error_git_unreachable, Strings.Error, MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
                        toolStripOffline.Visible = true;
                    }
                }
                else
                {
                    // creating new Git
                    Repository.Init(Cfg["PassDirectory"], false);
                    toolStripOffline.Visible = true;
                }
            }
            else
            {
                // Do we do remote or not
                CheckOnline(true);
            }

            // Making sure core.autocrlf = true
            using (var repo = new Repository(Cfg["PassDirectory"]))
            {
                repo.Config.Set("core.autocrlf", true);
            }

            // Init GPG if needed
            string gpgfile = Cfg["PassDirectory"];
            gpgfile += "\\.gpg-id";
            // Check if we need to init the directory
            if (!File.Exists(gpgfile))
            {
                // ReSharper disable once AssignNullToNotNullAttribute
                Directory.CreateDirectory(Path.GetDirectoryName(gpgfile));
                var newKeySelect = new KeySelect();
                if (newKeySelect.ShowDialog() == DialogResult.OK)
                {
                    using (var w = new StreamWriter(gpgfile))
                    {
                        w.Write(newKeySelect.Gpgkey);
                    }
                    using (var repo = new Repository(Cfg["PassDirectory"]))
                    {
                        repo.Stage(gpgfile);
                        repo.Commit("gpgid added",
                            new Signature("pass4win", "pass4win", DateTimeOffset.Now),
                            new Signature("pass4win", "pass4win", DateTimeOffset.Now));
                    }
                }
                else
                {
                    newKeySelect.Close();
                    MessageBox.Show(Strings.Error_nokey, Strings.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    Environment.Exit(1);
                }
            }
            // Setting the exe location for the GPG Dll
            GpgInterface.ExePath = Cfg["GPGEXE"];

            // init FileSystemInterface class
            fsi = new FileSystemInterface(Cfg["PassDirectory"]);

            // Fill tree
            CreateNodes();
    
            this.enableTray = true;
        }
Пример #8
0
        public frmMain()
        {
            InitializeComponent();
            // Checking for appsettings

            // Do we have a valid password store
            if (Properties.Settings.Default.PassDirectory == "firstrun")
            {
                if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
                {
                    Properties.Settings.Default.PassDirectory = folderBrowserDialog1.SelectedPath;
                }
                else
                {
                    MessageBox.Show("We need a place to store stuff. Restart the program and try again.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    System.Environment.Exit(1);
                }
            }

            // GPG exe location
            if (Properties.Settings.Default.GPGEXE == "firstrun")
            {
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    Properties.Settings.Default.GPGEXE = openFileDialog1.FileName;
                }
                else
                {
                    MessageBox.Show("We really need GPG2.exe. Restart the program and try again.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    System.Environment.Exit(1);
                }
            }

            //checking git status
            if (!Repository.IsValid(Properties.Settings.Default.PassDirectory))
            {
                string value = "";
                // Do we have a remote
                if (InputBox.Show("Enter the remote git repo or blank for no remote", "Remote Git (HTTPS):", ref value) == DialogResult.OK)
                {
                    Properties.Settings.Default.GitRemote = value;
                    if (Properties.Settings.Default.GitRemote != "")
                    {
                        // Get username and password
                        value = "";
                        if (InputBox.Show("Username", "Remote Username:"******"";
                            if (InputBox.Show("Password", "Remote Password:"******"Couldn't connect to remote git repository. Restart the program and try again.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                    System.Environment.Exit(1);
                                }
                                // save encrypted version of user and pass for git
                                Properties.Settings.Default.GitUser = EncryptConfig(GitUsername, "pass4win");
                                Properties.Settings.Default.GitPass = EncryptConfig(GitPassword, "pass4win");
                            }
                        }
                    }
                }
                // Checking if the remote is cloned succesfull
                if (!Repository.IsValid(Properties.Settings.Default.PassDirectory))
                {
                    // creating new Git
                    Repository.Init(Properties.Settings.Default.PassDirectory);
                    Properties.Settings.Default.GitUser = EncryptConfig("RandomGarbage", "pass4win");
                    Properties.Settings.Default.GitPass = EncryptConfig("RandomGarbage", "pass4win");
                }
            }
            else
            {
                // so we have a repository let's load the user/pass
                if (Properties.Settings.Default.GitUser != "")
                {
                    GitUsername = DecryptConfig(Properties.Settings.Default.GitUser, "pass4win");
                    GitPassword = DecryptConfig(Properties.Settings.Default.GitPass, "pass4win");
                }
                else
                {
                    string value = "";
                    if (InputBox.Show("Username", "Remote Username:"******"";
                        if (InputBox.Show("Password", "Remote Password:"******"We really need a username. Restart the program and try again.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        System.Environment.Exit(1);
                    }
                    if (GitPassword == null)
                    {
                        MessageBox.Show("We really need a password. Restart the program and try again.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        System.Environment.Exit(1);
                    }
                    Properties.Settings.Default.GitUser = EncryptConfig(GitUsername, "pass4win");
                    Properties.Settings.Default.GitPass = EncryptConfig(GitPassword, "pass4win");
                }

                // Check if we have the latest
                using (var repo = new Repository(Properties.Settings.Default.PassDirectory))
                {
                    Signature Signature = new Signature("pass4win","*****@*****.**", new DateTimeOffset(2011, 06, 16, 10, 58, 27, TimeSpan.FromHours(2)));
                    FetchOptions fetchOptions = new FetchOptions();
                    fetchOptions.CredentialsProvider = (_url, _user, _cred) => new UsernamePasswordCredentials
                                        {
                                            Username = GitUsername,
                                            Password = GitPassword
                                        };
                    MergeOptions mergeOptions = new MergeOptions();
                    PullOptions pullOptions = new PullOptions();
                    pullOptions.FetchOptions = fetchOptions;
                    pullOptions.MergeOptions = mergeOptions;
                    MergeResult mergeResult = repo.Network.Pull(Signature, pullOptions);
                }

            }

            // Init GPG if needed
            string gpgfile = Properties.Settings.Default.PassDirectory;
            gpgfile += "\\.gpg-id";
            // Check if we need to init the directory
            if (!File.Exists(gpgfile))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(gpgfile));
                KeySelect newKeySelect = new KeySelect();
                if (newKeySelect.ShowDialog() == DialogResult.OK)
                {
                    using (StreamWriter w = new StreamWriter(gpgfile))
                    {
                        w.Write(newKeySelect.gpgkey);
                    }
                    using (var repo = new Repository(Properties.Settings.Default.PassDirectory))
                    {
                        repo.Stage(gpgfile);
                        repo.Commit("gpgid added", new Signature("pass4win", "pass4win", System.DateTimeOffset.Now), new Signature("pass4win", "pass4win", System.DateTimeOffset.Now));
                    }
                }
                else
                {
                    MessageBox.Show("Need key...... Restart the program and try again.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    System.Environment.Exit(1);
                }
            }
            // Setting the exe location for the GPG Dll
            GpgInterface.ExePath = Properties.Settings.Default.GPGEXE;

            // saving settings
            Properties.Settings.Default.Save();

            // Setting up datagrid
            dt.Columns.Add("colPath", typeof(string));
            dt.Columns.Add("colText", typeof(string));

            ListDirectory(new DirectoryInfo(Properties.Settings.Default.PassDirectory), "");

            dataPass.DataSource = dt.DefaultView;
            dataPass.Columns[0].Visible=false;
        }
Пример #9
0
        /// <summary>
        /// Inits the repo, gpg etc
        /// </summary>
        public frmMain()
        {
            InitializeComponent();

            var bugsnag = new BaseClient("23814316a6ecfe8ff344b6a467f07171");

            EnableTray = false;

            // Getting actual version
            System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
            FileVersionInfo            fvi      = FileVersionInfo.GetVersionInfo(assembly.Location);
            string version = fvi.FileVersion;

            cfg["version"] = version.Remove(5, 2);

            this.Text = "Pass4Win " + Strings.Version + " " + cfg["version"];

            // checking for update this an async operation
            LatestPass4WinRelease();

            // Do we have a valid password store and settings
            try
            {
                if (cfg["PassDirectory"] == "")
                {
                    // this will fail, I know ugly hack
                }
            }
            catch
            {
                cfg["FirstRun"] = true;
                frmConfig Config       = new frmConfig();
                var       dialogResult = Config.ShowDialog();
            }
            //checking git status
            if (!LibGit2Sharp.Repository.IsValid(cfg["PassDirectory"]))
            {
                // Remote or generate a new one
                if (cfg["UseGitRemote"] == true)
                {
                    // check if server is alive
                    if (IsGITAlive(cfg["UseGitRemote"]) || IsHTTPSAlive(cfg["UseGitRemote"]))
                    {
                        // clone the repo and catch any error
                        try
                        {
                            string clonedRepoPath = LibGit2Sharp.Repository.Clone(cfg["GitRemote"], cfg["PassDirectory"], new CloneOptions()
                            {
                                CredentialsProvider = (_url, _user, _cred) => new UsernamePasswordCredentials
                                {
                                    Username = cfg["GitUser"],
                                    Password = DecryptConfig(cfg["GitPass"], "pass4win")
                                }
                            });
                        }
                        catch
                        {
                            MessageBox.Show(Strings.Error_connection, Strings.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                            toolStripOffline.Visible = true;
                        }
                    }
                    else
                    {
                        MessageBox.Show(Strings.Error_git_unreachable, Strings.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                        toolStripOffline.Visible = true;
                    }
                }
                else
                {
                    // creating new Git
                    var repo = LibGit2Sharp.Repository.Init(cfg["PassDirectory"], false);
                    toolStripOffline.Visible = true;
                }
            }
            else
            {
                // Do we do remote or not
                CheckOnline(true);
            }

            // Making sure core.autocrlf = true
            using (var repo = new LibGit2Sharp.Repository(cfg["PassDirectory"]))
            {
                repo.Config.Set("core.autocrlf", true);
            }

            // Init GPG if needed
            string gpgfile = cfg["PassDirectory"];

            gpgfile += "\\.gpg-id";
            // Check if we need to init the directory
            if (!File.Exists(gpgfile))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(gpgfile));
                KeySelect newKeySelect = new KeySelect();
                if (newKeySelect.ShowDialog() == DialogResult.OK)
                {
                    using (StreamWriter w = new StreamWriter(gpgfile))
                    {
                        w.Write(newKeySelect.gpgkey);
                    }
                    using (var repo = new LibGit2Sharp.Repository(cfg["PassDirectory"]))
                    {
                        repo.Stage(gpgfile);
                        repo.Commit("gpgid added", new LibGit2Sharp.Signature("pass4win", "pass4win", System.DateTimeOffset.Now), new LibGit2Sharp.Signature("pass4win", "pass4win", System.DateTimeOffset.Now));
                    }
                }
                else
                {
                    newKeySelect.Close();
                    MessageBox.Show(Strings.Error_nokey, Strings.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    System.Environment.Exit(1);
                }
            }
            // Setting the exe location for the GPG Dll
            GpgInterface.ExePath = cfg["GPGEXE"];

            // Setting up datagrid
            dt.Columns.Add("colPath", typeof(string));
            dt.Columns.Add("colText", typeof(string));

            treeDt.Columns.Add("colPath", typeof(string));
            treeDt.Columns.Add("colText", typeof(string));

            ListDirectory(new DirectoryInfo(cfg["PassDirectory"]), "");
            fillDirectoryTree(dirTreeView, cfg["PassDirectory"]);

            dataPass.DataSource         = dt.DefaultView;
            dataPass.Columns[0].Visible = false;

            EnableTray = true;
        }
Пример #10
0
        /// <summary>
        ///     Inits the repo, gpg etc
        /// </summary>
        public FrmMain(FileSystemInterface fileSystemInterface, KeySelect keySelect, ConfigHandling config)
        {
            log.Debug("Init variables");
            fsi = fileSystemInterface;
            _keySelect = keySelect;
            _config = config;
            InitializeComponent();
            toolStripStatusLabel1.Text = "";

            log.Debug("init Bugsnag");
            var bugsnag = new BaseClient("23814316a6ecfe8ff344b6a467f07171");

            this.enableTray = false;
            // Getting actual version
            log.Debug("Getting program version");
            var assembly = Assembly.GetExecutingAssembly();
            var fvi = FileVersionInfo.GetVersionInfo(assembly.Location);
            var version = fvi.FileVersion;
            _config["version"] = version.Remove(5);
            Text = @"Pass4Win " + Strings.Version + @" " + _config["version"];
            // check config input
            log.Debug("Checking config validity");
            try
            {
                bool ChkBool = Directory.Exists(_config["PassDirectory"]);
                if (!ChkBool)
                    throw new Exception("Pass directory fail");
                ChkBool = File.Exists(_config["GPGEXE"]);
                if (!ChkBool)
                    throw new Exception("GPG location fail");
                if (_config["UseGitRemote"] || _config["ExternalGit"])
                {
                    // nobody cares, we want the exception and it's auto generated due to a null value or not a boolean
                }
            }
            catch
            {
                // no matter if it's a first run or a corrupt entry, let them fix it.
                log.Debug("Config corrupt or first run");
                _config["FirstRun"] = true;
                Program.Scope.Resolve<FrmConfig>().ShowDialog();
            }

            // checking for update in the background
            log.Debug("Checking online for latest release");
            LatestPass4WinRelease();

            // making new git object
            if (_config["ExternalGit"] && !Program.NoGit)
            {
                log.Debug("Using an external git executable");
                GitRepo = new GitHandling(_config["PassDirectory"], _config["GitRemote"], _config["ExternalGitLocation"]);
            }
            else if (!Program.NoGit)
            {
                log.Debug("Using the internal git executable");
                GitRepo = new GitHandling(_config["PassDirectory"], _config["GitRemote"]);
            }

            //checking git status
            if (_config["UseGitRemote"] == true || _config["ExternalGit"])
            {
                if (!Program.NoGit)
                {
                    if (GitRepo.ConnectToRepo() || (_config["ExternalGit"]))
                    {
                        log.Debug("Remote Git is valid and active");
                        this.gitRepoOffline = false;
                        toolStripOffline.Visible = false;
                        if (!GitRepo.Fetch(_config["GitUser"], DecryptConfig(_config["GitPass"], "pass4win")))
                        {
                            // Something failed which shouldn't fail, rechecking online status
                            CheckOnline(false);
                        }
                    }
                    // checking if we can clone, otherwise make a new one
                    else if (!GitRepo.GitClone(_config["GitUser"], DecryptConfig(_config["GitPass"], "pass4win")))
                    {
                        log.Debug("Making a new git repo");
                        Repository.Init(_config["PassDirectory"], false);
                        GitRepo.ConnectToRepo();
                        toolStripOffline.Visible = true;
                    }
                    // no connection, no repo. So make a new one
                    else if (!GitHandling.IsValid(_config["PassDirectory"]))
                    {
                        log.Debug("Making a new git repo");
                        Repository.Init(_config["PassDirectory"], false);
                        GitRepo.ConnectToRepo();
                        toolStripOffline.Visible = true;
                    }
                }
            }

            // Init GPG if needed
            string gpgfile = _config["PassDirectory"];
            gpgfile += "\\.gpg-id";
            // Check if we need to init the directory
            if (!File.Exists(gpgfile))
            {
                log.Debug("Creating .gpg-id");
                // ReSharper disable once AssignNullToNotNullAttribute
                Directory.CreateDirectory(Path.GetDirectoryName(gpgfile));
                if (_keySelect.ShowDialog() == DialogResult.OK)
                {
                    using (var w = new StreamWriter(gpgfile))
                    {
                        w.Write(_keySelect.Gpgkey);
                    }

                    if (!Program.NoGit)
                    {
                        if (!GitRepo.Commit(gpgfile))
                        {
                            MessageBox.Show(
                                Strings.FrmMain_EncryptCallback_Commit_failed_,
                                Strings.Error,
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                            return;
                        }
                    }
                }
                else
                {
                    _keySelect.Close();
                    MessageBox.Show(Strings.Error_nokey, Strings.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    Environment.Exit(1);
                }
            }
            // Setting the exe location for the GPG Dll
            GpgInterface.ExePath = _config["GPGEXE"];

            // Fill tree
            CreateNodes();

            this.enableTray = true;
        }
Пример #11
0
        /// <summary>
        /// Inits the repo, gpg etc
        /// </summary>
        public frmMain()
        {
            InitializeComponent();

            var bugsnag = new BaseClient("23814316a6ecfe8ff344b6a467f07171");

            EnableTray = false;

            // Getting actual version
            System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
            FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(assembly.Location);
            string version = fvi.FileVersion;
            cfg["version"] = version.Remove(5, 2);

            this.Text = "Pass4Win " + Strings.Version + " " + cfg["version"];

            // checking for update this an async operation
            LatestPass4WinRelease();

            // Do we have a valid password store and settings
            try
            {
                if (cfg["PassDirectory"] == "")
                {
                    // this will fail, I know ugly hack
                }
            }
            catch
            {
                cfg["FirstRun"] = true;
                frmConfig Config = new frmConfig();
                var dialogResult = Config.ShowDialog();
                
            }
            //checking git status
            if (!LibGit2Sharp.Repository.IsValid(cfg["PassDirectory"]))
            {
                // Remote or generate a new one
                if (cfg["UseGitRemote"] == true)
                {
                    // check if server is alive
                    if (IsGITAlive(cfg["UseGitRemote"]) || IsHTTPSAlive(cfg["UseGitRemote"]))
                    {
                        // clone the repo and catch any error
                        try
                        {
                            string clonedRepoPath = LibGit2Sharp.Repository.Clone(cfg["GitRemote"], cfg["PassDirectory"], new CloneOptions()
                            {
                                CredentialsProvider = (_url, _user, _cred) => new UsernamePasswordCredentials
                                {
                                    Username = cfg["GitUser"],
                                    Password = DecryptConfig(cfg["GitPass"], "pass4win")
                                }
                            });
                        }
                        catch
                        {
                            MessageBox.Show(Strings.Error_connection, Strings.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                            toolStripOffline.Visible = true;
                        }
                    }
                    else
                    {
                        MessageBox.Show(Strings.Error_git_unreachable, Strings.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                        toolStripOffline.Visible = true;
                    }
                }
                else
                {
                    // creating new Git
                    var repo = LibGit2Sharp.Repository.Init(cfg["PassDirectory"], false);
                    toolStripOffline.Visible = true;
                }
            }
            else
            {
                // Do we do remote or not
                CheckOnline(true);
            }

            // Making sure core.autocrlf = true
            using (var repo = new LibGit2Sharp.Repository(cfg["PassDirectory"]))
            {
                repo.Config.Set("core.autocrlf", true);
            }

            // Init GPG if needed
            string gpgfile = cfg["PassDirectory"];
            gpgfile += "\\.gpg-id";
            // Check if we need to init the directory
            if (!File.Exists(gpgfile))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(gpgfile));
                KeySelect newKeySelect = new KeySelect();
                if (newKeySelect.ShowDialog() == DialogResult.OK)
                {
                    using (StreamWriter w = new StreamWriter(gpgfile))
                    {
                        w.Write(newKeySelect.gpgkey);
                    }
                    using (var repo = new LibGit2Sharp.Repository(cfg["PassDirectory"]))
                    {
                        repo.Stage(gpgfile);
                        repo.Commit("gpgid added", new LibGit2Sharp.Signature("pass4win", "pass4win", System.DateTimeOffset.Now), new LibGit2Sharp.Signature("pass4win", "pass4win", System.DateTimeOffset.Now));
                    }
                }
                else
                {
                    newKeySelect.Close();
                    MessageBox.Show(Strings.Error_nokey, Strings.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    System.Environment.Exit(1);
                }
            }
            // Setting the exe location for the GPG Dll
            GpgInterface.ExePath = cfg["GPGEXE"];

            // Setting up datagrid
            dt.Columns.Add("colPath", typeof(string));
            dt.Columns.Add("colText", typeof(string));

            treeDt.Columns.Add("colPath", typeof(string));
            treeDt.Columns.Add("colText", typeof(string));

            ListDirectory(new DirectoryInfo(cfg["PassDirectory"]), "");
            fillDirectoryTree(dirTreeView, cfg["PassDirectory"]);

            dataPass.DataSource = dt.DefaultView;
            dataPass.Columns[0].Visible = false;

            EnableTray = true;
        }