public override void Authenticate(RequestContext<AuthenticateRequest, AuthenticateResponse> requestContext) { PeerCred credentials = new PeerCred(((UnixSocketConnection)ServerContext.Connection).UnixSocket); if(credentials == null) { AuthenticateResponse response = requestContext.CreateResponse(); response.Succeeded = false; response.CustomErrorMessage = "Could not retrieve credentials from requesting client!"; response.Execute(); } else { ServerContext.ServerAuthenticationContext.AuthenticatedPermissionMember = new ExternalUser(credentials.UserID.ToString(), credentials.GroupID.ToString()); AuthenticateResponse response = requestContext.CreateResponse(); response.Succeeded = true; response.Execute(); } }
//TODO: complete this test daemon/server example, and a client //TODO: maybe generalise it and integrate it into the core public static void Main (string[] args) { bool isServer; if (args.Length == 1 && args[0] == "server") isServer = true; else if (args.Length == 1 && args[0] == "client") isServer = false; else { Console.Error.WriteLine ("Usage: test-server [server|client]"); return; } string addr = "unix:abstract=/tmp/dbus-ABCDEFGHIJ"; Connection conn; ObjectPath myOpath = new ObjectPath ("/org/ndesk/test"); string myNameReq = "org.ndesk.test"; if (!isServer) { conn = new Connection (addr); DemoObject demo = conn.GetObject<DemoObject> (myNameReq, myOpath); demo.GiveNoReply (); //float ret = demo.Hello ("hi from test client", 21); float ret = 200; while (ret > 5) { ret = demo.Hello ("hi from test client", (int)ret); Console.WriteLine ("Returned float: " + ret); System.Threading.Thread.Sleep (1000); } } else { string path; bool abstr; Address.Parse (addr, out path, out abstr); AbstractUnixEndPoint ep = new AbstractUnixEndPoint (path); Socket server = new Socket (AddressFamily.Unix, SocketType.Stream, 0); server.Bind (ep); //server.Listen (1); server.Listen (5); while (true) { Console.WriteLine ("Waiting for client on " + addr); Socket client = server.Accept (); Console.WriteLine ("Client accepted"); client.Blocking = true; PeerCred pc = new PeerCred (client); Console.WriteLine ("PeerCred: pid={0}, uid={1}, gid={2}", pc.ProcessID, pc.UserID, pc.GroupID); conn = new Connection (null); conn.ns = new NetworkStream (client); conn.SocketHandle = (long)client.Handle; //ConnectionHandler.Handle (conn); //in reality a thread per connection is of course too expensive ConnectionHandler hnd = new ConnectionHandler (conn); new Thread (new ThreadStart (hnd.Handle)).Start (); Console.WriteLine (); } } }