public inbox for cygwin-apps@cygwin.com
 help / color / mirror / Atom feed
From: Jon Turney <jon.turney@dronecode.org.uk>
To: "cygwin-apps@cygwin.com" <cygwin-apps@cygwin.com>,
	Christian Franke <Christian.Franke@t-online.de>
Subject: Re: [PATCH setup] Add new option --chown-admin
Date: Fri, 26 Aug 2022 14:27:33 +0100	[thread overview]
Message-ID: <80661301-d584-3af0-e588-1ec10f3b4108@dronecode.org.uk> (raw)
In-Reply-To: <038c3558-b424-3e4b-9de6-bd3eb6147406@t-online.de>

[-- Attachment #1: Type: text/plain, Size: 2327 bytes --]

On 23/08/2022 18:27, Christian Franke wrote:
> Jon Turney wrote:
>> On 12/07/2022 13:50, Jon Turney wrote:
>>> [Replying to the right list this time...]
>>> On 09/07/2022 13:21, Christian Franke wrote:
>> [...]
>>>>
>>>> The UserSettings ctor has a somewhat hidden side effect which sets 
>>>> root_scope correctly:
>>>>
>>>>   UserSettings::UserSettings(...);
>>>>    open_settings("setup.rc", ...);
>>>>     io_stream::open("cygfile:///etc/setup/setup.rc", ...);
>>>> io_stream_cygfile::io_stream_cygfile("/etc/setup/setup.rc", ...);
>>>>       get_root_dir_now();
>>>>        read_mounts("");
>>>>         read_mounts_nt("");
>>>>          root_scope = isuser ? IDC_ROOT_USER : IDC_ROOT_SYSTEM;
>>>>
>>>> Conclusion: Regression introduced Feb 24, 2012 (befc9dd).
>>>>
>>>
>>> Thanks for tracking this down.
>>>
>>> That just seems... fractally wrong.
>>
>> I kind of lost track of this.  Is there anything else needed to fix 
>> the original problem here?  Or is it solved by the change to defer 
>> setting the group until after root_scope is known?
> 
> The group seems to be correctly set now.
> 
> An old problem still remains: root_scope always ends up as 
> IDC_ROOT_SYSTEM if setup is run elevated, regardless off GUI setting. 
> Apply the temporary patch from here to see what happens:
> https://sourceware.org/pipermail/cygwin-apps/2022-July/042151.html

Ah, right, I remember now.

I think having read_mounts() have a side effect of setting root_scope is 
just nonsense now (it might have made some sense back in the day when 
the mount table was also stored in the registry).

So, how about the attached?

(We should perhaps also set the installation root directory to something 
other than C:\cygwin64 if a non-admin user, since they are unlikely to 
be able to write there in current windows versions, but that's difficult 
from a sequencing point of view in that dialog, and for backwards 
compatibility)

> Possibly difficult to fix, in particular in conjunction with later 
> changes via [< Back] button. An easier approach: Remove the GUI setting 
> and connect root_scope to -B option.

Looking for that option is not correct, because we might be running 
without -B, but from an already elevated shell.  I think checking 
nt_sec.isRunAsAdmin() would be the correct test.

[-- Attachment #2: 0001-Drop-setting-root_scope-as-a-side-effect-of-read_mou.patch --]
[-- Type: text/plain, Size: 3589 bytes --]

From 8bb9d2d647c4a9c49e696efb14d976682ad28647 Mon Sep 17 00:00:00 2001
From: Jon Turney <jon.turney@dronecode.org.uk>
Date: Wed, 24 Aug 2022 13:31:49 +0100
Subject: [PATCH setup] Drop setting root_scope as a side-effect of
 read_mounts()

Default root_scope as appropriate, allowing GUI to override it
Only enable 'Install For All Users' if we have Admin privs
Drop some comment cruft
---
 mount.cc | 30 +++++++++++++++++++++++-------
 mount.h  |  9 +--------
 root.cc  | 12 ++++++++++++
 3 files changed, 36 insertions(+), 15 deletions(-)

diff --git a/mount.cc b/mount.cc
index 0136396..a38f52c 100644
--- a/mount.cc
+++ b/mount.cc
@@ -140,10 +140,6 @@ create_install_root ()
 						      : "LOCAL_MACHINE\\")
 		      << buf << "\\rootdir = \"" << get_root_dir () << "\""
 		      << endLog;
-
-  // The mount table is already in the right shape at this point.
-  // Reading it again is not necessary.
-  //read_mounts (std::string ());
 }
 
 inline char *
@@ -316,8 +312,6 @@ read_mounts (const std::string val)
     }
   got_usr_bin = got_usr_lib = false;
 
-  root_scope = (nt_sec.isRunAsAdmin ())? IDC_ROOT_SYSTEM : IDC_ROOT_USER;
-
   if (val.size ())
     {
       /* Cygwin rootdir always < MAX_PATH. */
@@ -353,7 +347,6 @@ read_mounts (const std::string val)
 	    {
 	      m->native = std::string (aBuffer);
 	      m->posix = "/";
-	      root_scope = isuser ? IDC_ROOT_USER : IDC_ROOT_SYSTEM;
 	      root_here = m++;
 	      from_fstab (m, root_here->native);
 	      add_usr_mnts (m);
@@ -376,6 +369,29 @@ read_mounts (const std::string val)
     }
 }
 
+// set default root_scope: USER if only HKEY_CURRENT_USER registry key exists,
+// otherwise SYSTEM.
+void set_default_root_scope()
+{
+  root_scope = IDC_ROOT_SYSTEM;
+
+  char buf[10000];
+  for (int isuser = 0; isuser <= 1; isuser++)
+    {
+      snprintf (buf, sizeof(buf), "Software\\%s\\%s",
+                CYGWIN_INFO_CYGWIN_REGISTRY_NAME,
+                CYGWIN_INFO_CYGWIN_SETUP_REGISTRY_NAME);
+      HKEY key = isuser ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE;
+      if (RegOpenKeyEx (key, buf, 0, KEY_ALL_ACCESS | SETUP_KEY_WOW64,
+                        &key) == ERROR_SUCCESS)
+        {
+          RegCloseKey (key);
+          root_scope = isuser ? IDC_ROOT_USER : IDC_ROOT_SYSTEM;
+          break;
+        }
+    }
+}
+
 void
 set_root_dir (const std::string val)
 {
diff --git a/mount.h b/mount.h
index a7d7e39..c451a02 100644
--- a/mount.h
+++ b/mount.h
@@ -15,11 +15,6 @@
 
 #ifndef SETUP_MOUNT_H
 #define SETUP_MOUNT_H
-
-/* Finds the existing root mount, or returns NULL.  istext is set to
-   nonzero if the existing mount is a text mount, else zero for
-   binary. */
-
 #include <string>
 #include "String++.h"
 
@@ -27,9 +22,7 @@
 
 void create_install_root ();
 void read_mounts (const std::string);
-
-/* Sets the cygdrive flags.  Used to make the automounted drives' binary/text
-mode consistent with the standard Cygwin mounts. */
+void set_default_root_scope();
 
 std::string cygpath (const std::string&);
 void set_root_dir (const std::string);
diff --git a/root.cc b/root.cc
index 9f072d7..ccbd6ae 100644
--- a/root.cc
+++ b/root.cc
@@ -259,6 +259,18 @@ RootPage::OnInit ()
   if (!get_root_dir ().size())
     read_mounts (std::string ());
   orig_root_dir = get_root_dir();
+
+  if (!nt_sec.isRunAsAdmin())
+    {
+      // disable IDC_ROOT_SYSTEM if not running as admin
+      EnableWindow(GetDlgItem(IDC_ROOT_SYSTEM), FALSE);
+      root_scope = IDC_ROOT_USER;
+    }
+  else
+    {
+      set_default_root_scope();
+    }
+
   load_dialog (GetHWND ());
 }
 
-- 
2.37.2


  reply	other threads:[~2022-08-26 13:27 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-06  7:14 Christian Franke
2022-07-06 13:53 ` Jon Turney
2022-07-06 16:34   ` Christian Franke
2022-07-07 11:38     ` Jon Turney
2022-07-07 14:45       ` Christian Franke
2022-07-07 14:59         ` Christian Franke
     [not found]         ` <d7d51d1c-f6d5-2fac-3e6d-86714efd0734@dronecode.org.uk>
     [not found]           ` <32655945-5075-0823-2a1d-b72caa4b7791@t-online.de>
2022-07-12 12:50             ` Jon Turney
2022-08-23 15:20               ` Jon Turney
2022-08-23 17:27                 ` Christian Franke
2022-08-26 13:27                   ` Jon Turney [this message]
2022-08-26 15:02                     ` Christian Franke
2022-08-28 17:33                       ` Christian Franke
2022-09-02 13:56                         ` Jon Turney
2022-09-02 15:17                           ` Christian Franke
2022-09-15 17:45                             ` Jon Turney
2022-10-04 12:05                               ` Christian Franke
2022-11-29 21:37                                 ` Jon Turney
2022-11-30 18:49                                   ` Christian Franke

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=80661301-d584-3af0-e588-1ec10f3b4108@dronecode.org.uk \
    --to=jon.turney@dronecode.org.uk \
    --cc=Christian.Franke@t-online.de \
    --cc=cygwin-apps@cygwin.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).