Exemplo n.º 1
0
        public static void UnloadKey(SshConnectionData sftpConnectionData, string clientName, string authorizedKeyPath)
        {
            lock (locker)
            {
                // Inserire quì la connessione ssh e salvataggio della chiave pubblica sul server
                // Lo faccio tramite SSH, in questo modo è possibile tenere webserver e server ssh anche separati
                SftpHelper sftp = new SftpHelper();

                // download authorized_keys
                sftp.DownloadFile(sftpConnectionData, authorizedKeyPath, Constants.TEMP_AUTHORIZED_KEYS_FILENAME);

                // check if authorized keys already contains the client key
                bool clientCurrentlyLoaded = IsClientCurrentlyLoaded(clientName);

                // aggiunta della nuova chiave ad authorized_keys
                if (clientCurrentlyLoaded)
                {
                    Console.WriteLine($"Unloading {clientName} key.");

                    RemoveAuthorizedKey(clientName);

                    // upload di authorized_keys
                    sftp.UploadFile(sftpConnectionData, Constants.TEMP_AUTHORIZED_KEYS_FILENAME, authorizedKeyPath);
                }
            }
        }
Exemplo n.º 2
0
        private static UploadResult UploadExportCsvToSftp(string remoteDirectory, string uploadFileNamePath)
        {
            var result = new UploadResult
            {
                SourceFileNamePath      = uploadFileNamePath,
                DestinationFileNamePath = Path.Combine(remoteDirectory, Path.GetFileName(uploadFileNamePath))
            };

            if (SftpHelper.CreateDirectory(remoteDirectory))
            {
                if (SftpHelper.UploadFile(result.DestinationFileNamePath, uploadFileNamePath, out string errorMessage))
                {
                    result.Success        = true;
                    result.SuccessMessage = "The upload has completed successfully";
                }
                else
                {
                    result.Success      = false;
                    result.ErrorMessage = errorMessage;
                }
            }
            else
            {
                result.Success      = false;
                result.ErrorMessage = "Unable to create upload folder in remote server";
            }

            return(result);
        }
Exemplo n.º 3
0
        public static void SaveKeys(SshConnectionData sftpConnectionData, string sshUsername, string clientName, string publicKey, string authorizedKeyPath, int?forwardingPort)
        {
            lock (locker)
            {
                // Inserire quì la connessione ssh e salvataggio della chiave pubblica sul server
                // Lo faccio tramite SSH, in questo modo è possibile tenere webserver e server ssh anche separati
                SftpHelper sftp = new SftpHelper();

                // download authorized_keys
                sftp.DownloadFile(sftpConnectionData, authorizedKeyPath, Constants.TEMP_AUTHORIZED_KEYS_FILENAME);

                #warning la verifica della presenza funziona, ma non è un metodo efficiente. forse andrebbe fatto a database prima di scaricare il file delle auth
                // check if authorized keys already contains the client key
                bool clientAlreadyEnabled = IsClientAlreadyEnabled(publicKey, clientName);

                // aggiunta della nuova chiave ad authorized_keys
                if (!clientAlreadyEnabled)
                {
                    AddKeyToAuthorized(publicKey, clientName, forwardingPort);
                    // upload di authorized_keys
                    sftp.UploadFile(sftpConnectionData, Constants.TEMP_AUTHORIZED_KEYS_FILENAME, authorizedKeyPath);
                }
            }
        }