Beispiel #1
0
        public void TestUnixAgentBadMessage()
        {
            const string socketFileName = "test2.socket";

            if (File.Exists(socketFileName))
            {
                File.Delete(socketFileName);
            }

            using (var agent = new UnixAgent()) {
                agent.StartUnixSocket(socketFileName);
                using (var client = new Mono.Unix.UnixClient(socketFileName))
                    using (var stream = client.GetStream()) {
                        var message = new byte [] { 0, 0, 0, 0 };
                        stream.Write(message, 0, message.Length); // send garbage
                        stream.Flush();
                        var reply = new byte [5];
                        stream.Read(reply, 0, reply.Length);
                        var expected = new byte [] {
                            0, 0, 0, 1,
                            (byte)Agent.Message.SSH_AGENT_FAILURE,
                        };
                        Assert.That(reply, Is.EqualTo(expected));
                    }
            }
        }
Beispiel #2
0
        public void TestUnixAgentGoodMessage()
        {
            const string socketFileName = "test3.socket";

            if (File.Exists(socketFileName))
            {
                File.Delete(socketFileName);
            }

            using (var agent = new UnixAgent()) {
                agent.StartUnixSocket(socketFileName);
                using (var client = new Mono.Unix.UnixClient(socketFileName))
                    using (var stream = client.GetStream()) {
                        var message = new byte [] {
                            0, 0, 0, 1,
                            (byte)Agent.Message.SSH1_AGENTC_REQUEST_RSA_IDENTITIES,
                        };
                        stream.Write(message, 0, message.Length); // send message
                        stream.Flush();
                        var reply = new byte [9];
                        stream.Read(reply, 0, reply.Length);
                        var expected = new byte [] {
                            0, 0, 0, 5,
                            (byte)Agent.Message.SSH1_AGENT_RSA_IDENTITIES_ANSWER,
                            0, 0, 0, 0,
                        };
                        Assert.That(reply, Is.EqualTo(expected));
                    }
            }
        }
Beispiel #3
0
		static IDisposable ListenUnix (string path, AsyncCallback accept)
		{
			var socket = new UnixClient ();
			var client = socket.Client;
			client.Bind (new UnixEndPoint (path));
			client.Listen (1);
			client.BeginAccept (accept, client);
			return socket;
		}
Beispiel #4
0
		public void TestPassUnixFd ()
		{
			using (ListenUnix (FASTCGI_SOCKET_PATH, FastCgiAccept))
			using (ListenUnix (FPM_SOCKET_PATH, FpmUnixAccept)) {
				var webserver = new UnixClient (FPM_SOCKET_PATH);
				string read;
				using (var stream = webserver.GetStream ())
				using (var reader = new StreamReader(stream))
					read = reader.ReadLine ();
				Assert.AreEqual (MESSAGE, read);
			}
		}
Beispiel #5
0
		public override void Close ()
		{
			bool previously_closed = this.IsClosed;

			// Important to set this before we close the
			// UnixClient, since that will trigger the
			// ReadCallback() method, reading 0 bytes off the
			// wire, and we check this.closed in there.
			this.IsClosed = true;

			if (client != null) {
				client.Close ();
				client = null;
			}

			if (!previously_closed)
				InvokeClosedEvent ();
		}
Beispiel #6
0
        public void TestUnixAgentBadMessage()
        {
            const string socketFileName = "test2.socket";

              if (File.Exists (socketFileName)) {
            File.Delete(socketFileName);
              }

              using (var agent = new UnixAgent(socketFileName))
              using (var client = new Mono.Unix.UnixClient(socketFileName))
              using (var stream = client.GetStream ()) {
            var message = new byte[] { 0, 0, 0, 0 };
            stream.Write(message, 0, message.Length); // send garbage
            stream.Flush();
            var reply = new byte[5];
            stream.Read(reply, 0, reply.Length);
            var expected = new byte [] {
              0, 0, 0, 1,
              (byte)Agent.Message.SSH_AGENT_FAILURE,
            };
            Assert.That(reply, Is.EqualTo(expected));
              }
        }
Beispiel #7
0
		/////////////////////////////////////////////////////////

		static bool CheckHelper ()
		{
			string storage_dir = PathFinder.GetRemoteStorageDir (false);

			if (storage_dir == null)
				return false;

			// FIXME: We shouldn't need to know the path to the helper socket.
			string socket_name = Path.Combine (storage_dir, "socket-helper");

			if (! File.Exists (socket_name))
				return false;

			// Open, and then immediately close, a connection to the helper's socket.
			try {
				UnixClient test_client;
				test_client = new UnixClient (socket_name);
				test_client.Close ();
				return true;
			} catch (Exception ex) {
				return false;
			}
		}
Beispiel #8
0
		/*
		 * Loads the specific agent assembly into this vm.
		 */
		public void Attach (string agent, string args) {
			string user = UnixUserInfo.GetRealUser ().UserName;

			// Check whenever the attach socket exists
			string socket_file = "/tmp/mono-" + user + "/.mono-" + pid;

			if (!File.Exists (socket_file)) {
				string trigger_file = "/tmp/.mono_attach_pid" + pid;
				FileStream trigger = null;

				try {
					trigger = File.Create (trigger_file);
					trigger.Close ();

					// Ask the vm to start the attach mechanism
					Syscall.kill ((int)pid, Signum.SIGQUIT);

					// Wait for the socket file to materialize
					int i;
					for (i = 0; i < 10; ++i) {
						if (File.Exists (socket_file))
							break;
						Thread.Sleep (100);
					}

					if (i == 10)
						throw new Exception (String.Format ("Runtime failed to create attach socket '{0}'.", socket_file));
				} finally {
					File.Delete (trigger_file);
				}
			}

			/* 
			 * We communicate with the agent inside the runtime using a simlified
			 * version of the .net remoting protocol.
			 */

			string path = "/tmp/mono-" + user + "/.mono-" + pid;

			UnixClient client = new UnixClient (path);

			NetworkStream stream = client.GetStream ();

			// Compose payload
			MemoryStream ms = new MemoryStream ();
			BinaryWriter writer = new BinaryWriter (ms);
			write_string (writer, "attach");
			write_string (writer, agent);
			write_string (writer, args);

			// Write header
			byte[] magic = new byte [] { (byte)'M', (byte)'O', (byte)'N', (byte)'O', 1, 0 };
			stream.Write (magic, 0, magic.Length);

			// Write payload length
			new BinaryWriter (stream).Write ((int)ms.Length);

			// Write payload
			stream.Write (ms.GetBuffer (), 0, (int)ms.Length);
		}
Beispiel #9
0
		protected override void SendRequest (RequestMessage request)
		{
			client = new UnixClient (this.socket_name);
			client.SendBufferSize = 4096;
			client.ReceiveBufferSize = 4096;
			NetworkStream stream = client.GetStream ();
			
			base.SendRequest (request, stream);
		}
Beispiel #10
0
		static void FpmTcpAccept(IAsyncResult res)
		{
			if (!res.IsCompleted)
				throw new ArgumentException("res");

			var socket = res.AsyncState as TcpListener;
			if (socket == null)
				throw new ArgumentNullException ("state");

			using (var connection = socket.EndAcceptSocket (res)) {
				var back = new UnixClient (FASTCGI_SOCKET_PATH);
				SocketPassing.SendTo (back.Client, connection.Handle);
			}
		}
Beispiel #11
0
        public void TestUnixAgentGoodMessage()
        {
            const string socketFileName = "test3.socket";

              if (File.Exists(socketFileName)) {
            File.Delete(socketFileName);
              }

              using (var agent = new UnixAgent(socketFileName))
              using (var client = new Mono.Unix.UnixClient(socketFileName))
              using (var stream = client.GetStream()) {
            var message = new byte[] {
              0, 0, 0, 1,
              (byte)Agent.Message.SSH1_AGENTC_REQUEST_RSA_IDENTITIES,
            };
            stream.Write(message, 0, message.Length); // send message
            stream.Flush();
            var reply = new byte[9];
            stream.Read(reply, 0, reply.Length);
            var expected = new byte[] {
              0, 0, 0, 5,
              (byte)Agent.Message.SSH1_AGENT_RSA_IDENTITIES_ANSWER,
              0, 0, 0, 0,
            };
            Assert.That(reply, Is.EqualTo(expected));
              }
        }
Beispiel #12
0
		public override void Close ()
		{
			CancelIfBlocking ();

			// It's important that we abort the thread before we
			// grab the lock here and close the underlying
			// UnixClient, or else we'd deadlock between here and
			// the Read() in HandleConnection()

			// Do this in a lock since Close() should be thread-safe (can be called from AsyncCallback handler)
			lock (this.client_lock) {
				if (closed)
					return;

				if (this.client != null) {
					this.client.Close ();
					this.client = null;
				}

				if (this.executor != null) {
					this.executor.Cleanup ();
					this.executor.AsyncResponseEvent -= OnAsyncResponse;
					this.executor = null;
				}

			    closed = true;
			}

			Server.RunGC ();
		}
Beispiel #13
0
		public UnixConnectionHandler (UnixClient client)
		{
			this.client = client;
			this.client.SendBufferSize = 4096;
			this.client.ReceiveBufferSize = 4096;
		}