Thursday, March 3, 2011

C# redirect standardinput with PGP -ka command

Hi all,

I am having a problem which seems really daft. I must be missing something silly. We have a PGP keyring that is on one of our production servers. The user account it belongs to is not allowed to be logged on as interactively for security. Our problem is we sometimes need to add new keys and can not do this easily. So we thought we could create a quick console app that would be run as its ID and would call the PGP commands via the command line.

The command gets called but it asks for input to confirm what we are doing. Our problem is the "y" we send to standardinput is never displayed and the key is not verified.

here is the code:

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using System.DirectoryServices;
using System.Threading;

namespace TestConsoleApp
{
    class RegExValidator
    {
        private System.Diagnostics.Process myProcess;

        public RegExValidator()
        {
        }

        public static void Main(string[] args)
        {
            RegExValidator myValidator = new RegExValidator();
            myValidator.InstallKeys("C:\\Test\\batch.asc", "batch.asc");
        }


        private void InstallKeys(string keyPath, string keyName)
        {
            myProcess = new System.Diagnostics.Process();
            myProcess.StartInfo.RedirectStandardInput = true;
            myProcess.StartInfo.CreateNoWindow = false;
            myProcess.StartInfo.UseShellExecute = false;
            myProcess.StartInfo.FileName = "pgp";
            myProcess.StartInfo.Arguments = "-ka " + keyPath + "";
            myProcess.Start();

            StreamWriter myInput = myProcess.StandardInput;
            myInput.AutoFlush = true;
            Thread.Sleep(3000);

            myInput.WriteLine("y");

            myInput.WriteLine(Environment.NewLine);

        }

    }

}

This is the output we get on the command line.

 C:\Test>TestConsoleApp.exe
 Pretty Good Privacy(tm) Version 6.5.2
 (c) 1999 Network Associates Inc.
 Uses the BSafe(tm) Toolkit, which is copyright RSA Data Security, Inc.
 Export of this software may be restricted by the U.S. government.

 WARNING: Environmental variable TZ is not       defined, so GMT timestamps
         may be wrong.  See the PGP User's Guide to properly define TZ

 Looking for new keys...
 DSS  2048/1024 0xDE053A3D 2007/05/29 Batch Interface <batch@netgiro.com>
 sig?           0xDE053A3D             (Unknown signator, can't be checked)

 keyfile contains 1 new keys. Add these keys to keyring ? (Y/n)
 C:\Test>

Can anyone help?

Thanks

EDIT

We tried this process but instead of PGP we just moved a file and we got the Y/N box and that worked. It would seem that you may not be able to do it with PGP. No idea why though.

From stackoverflow
  • The message

    keyfile contains 1 new keys. Add these keys to keyring ? (Y/n)
    

    suggests replying with an Uppercase Y. try changing your call to:

    myInput.WriteLine("Y");
    

    (I have no PGP installed for checking, but have encountered other command line interfaces that insisted on case.)

    Another thing to try is flushing stream buffers, which clears all buffers for the stream and causes any buffered data to be written to the underlying device:

    myInput.WriteLine("Y");
    myInput.Flush();
    
    Jon : Hi Gimel, Thanks for the reply but it did not work we got exactly the same output.
    gimel : Next thing to try is Stream.Flush

0 comments:

Post a Comment