Bug 1518639: Remove the ability to remote to a different user or application. r=jimm
authorDave Townsend <dtownsend@oxymoronical.com>
Thu, 31 Jan 2019 11:12:32 -0800
changeset 520635 940349bea8e88038b8f3727ea225b6ad9aa2bbd2
parent 520634 14d519134002121d40c8fe43384c66d7070420b0
child 520636 d1f158c2928f02fe725038bdda99b13c4366bc6c
push id10862
push userffxbld-merge
push dateMon, 11 Mar 2019 13:01:11 +0000
treeherdermozilla-beta@a2e7f5c935da [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjimm
bugs1518639
milestone67.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1518639: Remove the ability to remote to a different user or application. r=jimm Remoting to a different user isn't supported everywhere and being able to remote to a different application entirely is kind of odd. I don't think it makes sense to continue to support these operations. Differential Revision: https://phabricator.services.mozilla.com/D19066
toolkit/xre/nsAppRunner.cpp
widget/xremoteclient/DBusRemoteClient.cpp
widget/xremoteclient/DBusRemoteClient.h
widget/xremoteclient/XRemoteClient.cpp
widget/xremoteclient/XRemoteClient.h
widget/xremoteclient/nsRemoteClient.h
--- a/toolkit/xre/nsAppRunner.cpp
+++ b/toolkit/xre/nsAppRunner.cpp
@@ -1541,50 +1541,18 @@ static inline void DumpVersion() {
 
   if (gAppData->copyright) {
     printf(", %s", (const char*)gAppData->copyright);
   }
   printf("\n");
 }
 
 #if defined(MOZ_WIDGET_GTK)
-static RemoteResult ParseRemoteCommandLine(nsCString& program,
-                                           const char** profile,
-                                           const char** username) {
-  ArgResult ar;
-
-  ar = CheckArg("p", profile, CheckArgFlag::None);
-  if (ar == ARG_BAD) {
-    // Leave it to the normal command line handling to handle this situation.
-    return REMOTE_NOT_FOUND;
-  }
-
-  const char* temp = nullptr;
-  ar = CheckArg("a", &temp, CheckArgFlag::CheckOSInt | CheckArgFlag::RemoveArg);
-  if (ar == ARG_BAD) {
-    PR_fprintf(PR_STDERR, "Error: argument -a requires an application name\n");
-    return REMOTE_ARG_BAD;
-  }
-  if (ar == ARG_FOUND) {
-    program.Assign(temp);
-  }
-
-  ar = CheckArg("u", username,
-                CheckArgFlag::CheckOSInt | CheckArgFlag::RemoveArg);
-  if (ar == ARG_BAD) {
-    PR_fprintf(PR_STDERR, "Error: argument -u requires a username\n");
-    return REMOTE_ARG_BAD;
-  }
-
-  return REMOTE_FOUND;
-}
-
 static RemoteResult StartRemoteClient(const char* aDesktopStartupID,
-                                      nsCString& program, const char* profile,
-                                      const char* username) {
+                                      nsCString& program, const char* profile) {
   nsAutoPtr<nsRemoteClient> client;
 
   bool useX11Remote = GDK_IS_X11_DISPLAY(gdk_display_get_default());
 
 #  if defined(MOZ_ENABLE_DBUS)
   if (!useX11Remote) {
     client = new DBusRemoteClient();
   }
@@ -1593,17 +1561,17 @@ static RemoteResult StartRemoteClient(co
     client = new XRemoteClient();
   }
 
   nsresult rv = client ? client->Init() : NS_ERROR_FAILURE;
   if (NS_FAILED(rv)) return REMOTE_NOT_FOUND;
 
   nsCString response;
   bool success = false;
-  rv = client->SendCommandLine(program.get(), username, profile, gArgc, gArgv,
+  rv = client->SendCommandLine(program.get(), profile, gArgc, gArgv,
                                aDesktopStartupID, getter_Copies(response),
                                &success);
   // did the command fail?
   if (!success) return REMOTE_NOT_FOUND;
 
   // The "command not parseable" error is returned when the
   // nsICommandLineHandler throws a NS_ERROR_ABORT.
   if (response.EqualsLiteral("500 command not parseable"))
@@ -3983,46 +3951,26 @@ int XREMain::XRE_mainStartup(bool* aExit
       newInstance = (e && *e);
     }
   }
 
   if (!newInstance) {
     nsAutoCString program(gAppData->remotingName);
     ToLowerCase(program);
 
-    const char* username = getenv("LOGNAME");
     const char* profile = nullptr;
-
-    RemoteResult rr = ParseRemoteCommandLine(program, &profile, &username);
-    if (rr == REMOTE_ARG_BAD) {
-      return 1;
-    }
-
-    if (!username) {
-      struct passwd* pw = getpwuid(geteuid());
-      if (pw && pw->pw_name) {
-        // Beware that another call to getpwent/getpwname/getpwuid will
-        // overwrite pw, but we don't have such another call between here and
-        // when username is used last.
-        username = pw->pw_name;
-      }
-    }
+    // It doesn't matter if this succeeds or fails. A failure will be handled
+    // later.
+    CheckArg("p", &profile, CheckArgFlag::None);
 
     nsCOMPtr<nsIFile> mutexDir;
     rv = GetSpecialSystemDirectory(OS_TemporaryDirectory,
                                    getter_AddRefs(mutexDir));
     if (NS_SUCCEEDED(rv)) {
-      nsAutoCString mutexPath = program + NS_LITERAL_CSTRING("_");
-      // In the unlikely even that LOGNAME is not set and getpwuid failed, just
-      // don't put the username in the mutex directory. It will conflict with
-      // other users mutex, but the worst that can happen is that they wait for
-      // MOZ_XREMOTE_START_TIMEOUT_SEC during startup in that case.
-      if (username) {
-        mutexPath.Append(username);
-      }
+      nsAutoCString mutexPath = program;
       if (profile) {
         mutexPath.Append(NS_LITERAL_CSTRING("_") + nsDependentCString(profile));
       }
       mutexDir->AppendNative(mutexPath);
 
       rv = mutexDir->Create(nsIFile::DIRECTORY_TYPE, 0700);
       if (NS_SUCCEEDED(rv) || rv == NS_ERROR_FILE_ALREADY_EXISTS) {
         mRemoteLockDir = mutexDir;
@@ -4041,17 +3989,17 @@ int XREMain::XRE_mainStartup(bool* aExit
         NS_WARNING("Cannot lock XRemote start mutex");
       }
     }
 
     // Try to remote the entire command line. If this fails, start up normally.
     const char* desktopStartupIDPtr =
         mDesktopStartupID.IsEmpty() ? nullptr : mDesktopStartupID.get();
 
-    rr = StartRemoteClient(desktopStartupIDPtr, program, profile, username);
+    RemoteResult rr = StartRemoteClient(desktopStartupIDPtr, program, profile);
     if (rr == REMOTE_FOUND) {
       *aExitFlag = true;
       return 0;
     }
     if (rr == REMOTE_ARG_BAD) {
       return 1;
     }
   }
--- a/widget/xremoteclient/DBusRemoteClient.cpp
+++ b/widget/xremoteclient/DBusRemoteClient.cpp
@@ -45,19 +45,20 @@ nsresult DBusRemoteClient::Init() {
 
 void DBusRemoteClient::Shutdown(void) {
   MOZ_LOG(sRemoteLm, LogLevel::Debug, ("DBusRemoteClient::Shutdown"));
   // This connection is owned by libdbus and we don't need to close it
   mConnection = nullptr;
 }
 
 nsresult DBusRemoteClient::SendCommandLine(
-    const char *aProgram, const char *aUsername, const char *aProfile,
-    int32_t argc, char **argv, const char *aDesktopStartupID, char **aResponse,
-    bool *aWindowFound) {
+    const char *aProgram, const char *aProfile, int32_t argc, char **argv,
+    const char *aDesktopStartupID, char **aResponse, bool *aWindowFound) {
+  NS_ENSURE_TRUE(aProgram, NS_ERROR_INVALID_ARG);
+
   MOZ_LOG(sRemoteLm, LogLevel::Debug, ("DBusRemoteClient::SendCommandLine"));
 
   int commandLineLength;
   char *commandLine =
       ConstructCommandLine(argc, argv, aDesktopStartupID, &commandLineLength);
   if (!commandLine) return NS_ERROR_FAILURE;
 
   nsresult rv =
--- a/widget/xremoteclient/DBusRemoteClient.h
+++ b/widget/xremoteclient/DBusRemoteClient.h
@@ -12,19 +12,19 @@
 #include "nsString.h"
 
 class DBusRemoteClient : public nsRemoteClient {
  public:
   DBusRemoteClient();
   ~DBusRemoteClient();
 
   virtual nsresult Init() override;
-  virtual nsresult SendCommandLine(const char *aProgram, const char *aUsername,
-                                   const char *aProfile, int32_t argc,
-                                   char **argv, const char *aDesktopStartupID,
+  virtual nsresult SendCommandLine(const char *aProgram, const char *aProfile,
+                                   int32_t argc, char **argv,
+                                   const char *aDesktopStartupID,
                                    char **aResponse, bool *aSucceeded) override;
   void Shutdown();
 
  private:
   bool GetRemoteDestinationName(const char *aProgram, const char *aProfile,
                                 nsCString &aDestinationName);
   nsresult DoSendDBusCommandLine(const char *aProgram, const char *aProfile,
                                  const char *aBuffer, int aLength);
--- a/widget/xremoteclient/XRemoteClient.cpp
+++ b/widget/xremoteclient/XRemoteClient.cpp
@@ -1,16 +1,17 @@
 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* vim:expandtab:shiftwidth=2:tabstop=8:
  */
 /* vim:set ts=8 sw=2 et cindent: */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
+#include "nsDebug.h"
 #include "mozilla/ArrayUtils.h"
 #include "mozilla/IntegerPrintfMacros.h"
 #include "mozilla/Sprintf.h"
 #include "mozilla/Unused.h"
 #include "XRemoteClient.h"
 #include "RemoteUtils.h"
 #include "plstr.h"
 #include "prsystem.h"
@@ -134,30 +135,31 @@ static int HandleBadWindow(Display *disp
     sGotBadWindow = true;
     return 0;  // ignored
   }
 
   return (*sOldHandler)(display, event);
 }
 
 nsresult XRemoteClient::SendCommandLine(const char *aProgram,
-                                        const char *aUsername,
                                         const char *aProfile, int32_t argc,
                                         char **argv,
                                         const char *aDesktopStartupID,
                                         char **aResponse, bool *aWindowFound) {
+  NS_ENSURE_TRUE(aProgram, NS_ERROR_INVALID_ARG);
+
   MOZ_LOG(sRemoteLm, LogLevel::Debug, ("XRemoteClient::SendCommandLine"));
 
   *aWindowFound = false;
 
   // FindBestWindow() iterates down the window hierarchy, so catch X errors
   // when windows get destroyed before being accessed.
   sOldHandler = XSetErrorHandler(HandleBadWindow);
 
-  Window w = FindBestWindow(aProgram, aUsername, aProfile);
+  Window w = FindBestWindow(aProgram, aProfile);
 
   nsresult rv = NS_OK;
 
   if (w) {
     // ok, let the caller know that we at least found a window.
     *aWindowFound = true;
 
     // Ignore BadWindow errors up to this point.  The last request from
@@ -369,17 +371,16 @@ nsresult XRemoteClient::GetLock(Window a
     MOZ_LOG(sRemoteLm, LogLevel::Debug,
             ("window 0x%x unexpectedly destroyed.\n", (unsigned int)aWindow));
   }
 
   return rv;
 }
 
 Window XRemoteClient::FindBestWindow(const char *aProgram,
-                                     const char *aUsername,
                                      const char *aProfile) {
   Window root = RootWindowOfScreen(DefaultScreenOfDisplay(mDisplay));
   Window bestWindow = 0;
   Window root2, parent, *kids;
   unsigned int nkids;
 
   // Get a list of the children of the root window, walk the list
   // looking for the best window that fits the criteria.
@@ -417,50 +418,40 @@ Window XRemoteClient::FindBestWindow(con
     XFree(data_return);
 
     if (!(version >= 5.1 && version < 6)) continue;
 
     data_return = 0;
 
     if (status != Success || type == None) continue;
 
-    // If someone passed in a program name, check it against this one
-    // unless it's "any" in which case, we don't care.  If someone did
-    // pass in a program name and this window doesn't support that
-    // protocol, we don't include it in our list.
-    if (aProgram && strcmp(aProgram, "any")) {
-      Unused << XGetWindowProperty(
-          mDisplay, w, mMozProgramAtom, 0, (65536 / sizeof(long)), False,
-          XA_STRING, &type, &format, &nitems, &bytesafter, &data_return);
+    // Check that this window is from the right program.
+    Unused << XGetWindowProperty(
+        mDisplay, w, mMozProgramAtom, 0, (65536 / sizeof(long)), False,
+        XA_STRING, &type, &format, &nitems, &bytesafter, &data_return);
 
-      // If the return name is not the same as what someone passed in,
-      // we don't want this window.
-      if (data_return) {
-        if (strcmp(aProgram, (const char *)data_return)) {
-          XFree(data_return);
-          continue;
-        }
-
-        // This is actually the success condition.
+    // If the return name is not the same as this program name, we don't want
+    // this window.
+    if (data_return) {
+      if (strcmp(aProgram, (const char *)data_return)) {
         XFree(data_return);
-      } else {
-        // Doesn't support the protocol, even though the user
-        // requested it.  So we're not going to use this window.
         continue;
       }
+
+      // This is actually the success condition.
+      XFree(data_return);
+    } else {
+      // Doesn't support the protocol, even though the user
+      // requested it.  So we're not going to use this window.
+      continue;
     }
 
     // Check to see if it has the user atom on that window.  If there
     // is then we need to make sure that it matches what we have.
-    const char *username;
-    if (aUsername) {
-      username = aUsername;
-    } else {
-      username = PR_GetEnv("LOGNAME");
-    }
+    const char *username = PR_GetEnv("LOGNAME");
 
     if (username) {
       Unused << XGetWindowProperty(
           mDisplay, w, mMozUserAtom, 0, (65536 / sizeof(long)), False,
           XA_STRING, &type, &format, &nitems, &bytesafter, &data_return);
 
       // if there's a username compare it with what we have
       if (data_return) {
--- a/widget/xremoteclient/XRemoteClient.h
+++ b/widget/xremoteclient/XRemoteClient.h
@@ -9,29 +9,28 @@
 #include "nsRemoteClient.h"
 
 class XRemoteClient : public nsRemoteClient {
  public:
   XRemoteClient();
   ~XRemoteClient();
 
   virtual nsresult Init() override;
-  virtual nsresult SendCommandLine(const char *aProgram, const char *aUsername,
-                                   const char *aProfile, int32_t argc,
-                                   char **argv, const char *aDesktopStartupID,
+  virtual nsresult SendCommandLine(const char *aProgram, const char *aProfile,
+                                   int32_t argc, char **argv,
+                                   const char *aDesktopStartupID,
                                    char **aResponse, bool *aSucceeded) override;
   void Shutdown();
 
  private:
   Window CheckWindow(Window aWindow);
   Window CheckChildren(Window aWindow);
   nsresult GetLock(Window aWindow, bool *aDestroyed);
   nsresult FreeLock(Window aWindow);
-  Window FindBestWindow(const char *aProgram, const char *aUsername,
-                        const char *aProfile);
+  Window FindBestWindow(const char *aProgram, const char *aProfile);
   nsresult DoSendCommandLine(Window aWindow, int32_t argc, char **argv,
                              const char *aDesktopStartupID, char **aResponse,
                              bool *aDestroyed);
   bool WaitForResponse(Window aWindow, char **aResponse, bool *aDestroyed,
                        Atom aCommandAtom);
 
   Display *mDisplay;
 
--- a/widget/xremoteclient/nsRemoteClient.h
+++ b/widget/xremoteclient/nsRemoteClient.h
@@ -48,15 +48,15 @@ class nsRemoteClient {
    *
    * @param aResponse If there is a response, it will be here.  This
    * includes error messages.  The string is allocated using stdlib
    * string functions, so free it with free().
    *
    * @return true if succeeded, false if no running instance was found.
    *
    */
-  virtual nsresult SendCommandLine(const char *aProgram, const char *aUsername,
-                                   const char *aProfile, int32_t argc,
-                                   char **argv, const char *aDesktopStartupID,
+  virtual nsresult SendCommandLine(const char *aProgram, const char *aProfile,
+                                   int32_t argc, char **argv,
+                                   const char *aDesktopStartupID,
                                    char **aResponse, bool *aSucceeded) = 0;
 };
 
 #endif  // nsRemoteClient_h__